suppose i need to print the 3rd, 5th, 9th, 20th, 34th and so on...
问题:
回答1:
Rows, by position, have no real meaning in a database (at least I can't think of any). Why do you care its actual row location?
How do you know that 3rd record is the record you want to delete? Match an ID, match a phrase in another field, do something to uniquely identify the record as the record you want to access. Doing this by it row location will not do this for you.
If you have five records that all match exactly and you only want to delete one then add the LIMIT 1 to the end of your DELETE statement. If they are exact the row doesn't matter. The 3rd or 5th or 7th wouldn't matter since they match your query exactly. So delete one of your matching query.
Now it sounds as if you are showing users a table of data in some order, which may or may not be duplicated, and allowing them the option to delete a row. You are taking the actual row location instead of storing the Primary Key with each displayed row. Then when a user selects a delete, you would just use the attached Primary Key to the row and delete that row.
You say "the primary key with several other fields are the same" - That can't be true as you can't have to identical primary keys. I'm now thinking you have some flaw in your database design. Post a table schema and sample of exactly what you are doing. No point continuing if you backed yourself into a corner with the design (which is sounds like)
(I can't comment on other posts so updates have to be edited in this post)
回答2:
You can also use a row number variable:
SELECT * FROM ( SELECT t.*, @rownum = @rownum + 1 AS rank FROM TABLE t, (SELECT @rownum := 0) r ORDER BY ID LIMIT 0, 34 ) Q WHERE rank in (3, 5, 9, 20, 34)
I'd try to use the LIMIT
smartly otherwise it will scan your whole table every time.
Based on your description of your scenario...
my scenario: I need to delete only a single row... there are several identical rows.. so using a delete with a where clause would delete all of them. Also, say there are 5 identical rows.. and i need to delete the third of those 5 rows...
You really need to be careful with this otherwise you delete the wrong rows. Think of a good criterium that would distinguish the rows that you want to delete from the other rows, say.. do you want to delete the newest, or all except the newest...
You can use an extended version of the @rownum trick to delete the Nth from each group.. but before I go into that it would be better to know more about your specific problem.
Ah now here comes the monkey out of the sleeve:
Suppose there are 5 identical rows.. the user clicks on the 3 rd row and asks that to be deleted... a where query won't be efficient as it deletes the first row that matches the where clause or all rows that match it.. how is that i delete only the third row and leave all others intact..???
Forget all I said. You need an AUTO_INCREMENT
field in your table, uniquely identifying your records. Then you can use DELETE FROM table WHERE id = $id
. Thanks to OMG Ponies for the OP therapy ;)
回答3:
OFFSET part of LIMIT clause works with a SELECT query.
What do you want to do??
回答4:
Rows in an SQL table are not in any particular order. You can order them by a specific field, like the ID which I assume is an auto increment value. If you want the second row when sorted by ID, you can do a query such as
SELECT * FROM MyTable ORDER BY ID LIMIT 1,1
To get row 19, you would do
SELECT * FROM MyTable ORDER BY ID LIMIT 18,1
Use the limit statement back a particular set of rows. If you want rows 10 through 19, you can use
SELECT * FROM MyTable ORDER BY ID LIMIT 9,10
In the limit statement, the first number represents the row number (0 based) and the second number is the number of rows to return.