MySQL – 选择记录的行号
作者:互联网
我在MySQL中有一个表填充如下.现在我需要按排序顺序选择记录的行号.例如,以“c”开头的单词的行数应为4.
Words
=====
coffee
banana
apple
cherry
blackberry
我尝试了以下查询,但结果错了.这里的dict是表名,单词是列名.
SELECT @rownum:=@rownum + 1 id FROM (SELECT * FROM dict ORDER BY words) d,(SELECT @rownum:=0) r WHERE d.words LIKE CONCAT('c','%')
对于上面的查询,我得到外部查询的行号.但我想要内部查询的行号.我不知道该怎么做.
任何帮助表示赞赏.谢谢.
解决方法:
试试吧:
SET @rownum = 0;
SELECT id
FROM (SELECT *, @rownum:=@rownum + 1 AS id FROM dict ORDER BY words) d
WHERE d.words LIKE CONCAT('c','%')
作为单个查询,请尝试:
SELECT id
FROM (SELECT *, @rownum:=@rownum + 1 AS id FROM dict, (SELECT @rownum:=0) r ORDER BY words) d
WHERE d.words LIKE CONCAT('c','%')
标签:row-number,mysql 来源: https://codeday.me/bug/20190726/1540990.html