This question already has an answer here:
这个问题在这里已有答案:
- grabbing first row in a mysql query only 4 answers
抓取mysql查询中的第一行只有4个答案
I have two tables table1 which has id(primary key)and table2 which has an name, id(foreign key). All ids are null initially in table2. Now whenever a new id is generated in parent table we have to select the very first row from table2 which has id null and update that row with the new id. Now select * from table2 where id is null
fetches a set of rows but is there a way to modify this sql query so that it fetches me the very first row of table2 where id column is null.
我有两个表table1有id(主键)和table2有一个名称,id(外键)。表2中的所有ID最初都为null。现在,只要在父表中生成新的id,我们就必须从table2中选择id为null的第一行,并使用新的id更新该行。现在从table2中选择*,其中id为null,获取一组行但是有一种方法可以修改这个sql查询,以便它获取table2的第一行,其中id列为null。
2 个解决方案
#1
2
select * from table2 where id is null limit 1
Limit specifies number of records you want to fetch.
限制指定要获取的记录数。
#2
0
SELECT * FROM table2 WHERE id is null LIMIT (n - 1), 1
example: SELECT * FROM table2 WHERE id is null LIMIT 2, 1
示例:SELECT * FROM table2 WHERE id为null LIMIT 2,1
#1
2
select * from table2 where id is null limit 1
Limit specifies number of records you want to fetch.
限制指定要获取的记录数。
#2
0
SELECT * FROM table2 WHERE id is null LIMIT (n - 1), 1
example: SELECT * FROM table2 WHERE id is null LIMIT 2, 1
示例:SELECT * FROM table2 WHERE id为null LIMIT 2,1