How can I get the offset of a particular row in MySQL?

Deadly 提交于 2021-02-07 19:39:22

问题


I'm trying to make an image database which does not keep a consistent record of ID's. For example it might go 1,2,6,7,12, but as you can see that is only 5 rows. Inside the table I have fileid and filename. I created a PHP script to show me the image when I give the fileid. But if I give it the ID 5 which does not exist I get an error. That's fine as I want an error for that, but not for users who will browse through these images using forward and back buttons. The forward and back buttons would need to retrieve the true fileid which comes after the given ID. Hopefully that makes sense.

This is how I imagine the code to look like:

SELECT offset( WHERE fileid=4 )

That would give me the offset of the row where fileid is equal to 4. I think this is easy enough to understand. The reasons I need this are for creating the forward and back button. So I planned to add 1 or take 1 from the offset which gives me the new ID, and the new filename. That way when users browse it will skip the dead ID values automatically, but it will give an error when giving a false ID.


回答1:


Going up:

SELECT * FROM table WHERE id > 'your_current_id' ORDER BY id LIMIT 1;

Going down:

SELECT * FROM table WHERE id < 'your_current_id' ORDER BY id DESC LIMIT 1;

ps: it is better to make LIMIT 2, so that you can see that you are at the first or at the last records in the database when only one record is returned.




回答2:


If your results are ordered by x, ascending, the following will give you your current offeset in the table:

SELECT COUNT(*) FROM tablename WHERE x < x_of_your_current_item;

If you just want to SELECT the next or previous row, you can skip having to do two queries by just directly selecting one row:

SELECT * FROM tablename WHERE x > x_of_your_current_item ORDER BY x LIMIT 1;

will give you the next item (and similarly < and adding DESC to the order-by would give you previous).




回答3:


You can use offset. Initially set offset as zero. First time your query will be SELECT * FROM TABLE order by table_id LIMIT 0,1

and next SELECT * FROM TABLE order by table_id LIMIT 1,1 .. and so on

This way one will get records from the beginning till the end.

Now about back and forward buttons.

  • Back Button: First time or whenever offset is zero disable back button

  • Forward Button: when you query for a current record you check for the next record too i.e. after this query SELECT * FROM TABLE order by table_id LIMIT 0,1 fire a query like this SELECT * FROM TABLE order by table_id LIMIT current_offset+1,1 and check if the query produces any results if it produces a result then set a boolean say next = TRUE else next = FALSE;

Using this boolean enable or disable Forward button.

One more thing on click of back button send the offset as current_offset - 1 and for forward button current_offset + 1

I hope this helps. I just came across this and thought of this solution.




回答4:


SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 50,10;
SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 60,10;
SELECT * FROM table WHERE ... ORDER BY id DESC LIMIT 70,10;


来源:https://stackoverflow.com/questions/9051578/how-can-i-get-the-offset-of-a-particular-row-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!