gpt4 book ai didi

php - 如何在选择查询中获取行号

转载 作者:行者123 更新时间:2023-12-01 00:40:13 25 4
gpt4 key购买 nike

我尝试此查询以获取选定行中的行号,但输出类似于 23,56,78,.... 我需要为每个选定行获取 1,2,3。请帮助我

SET @row=0;
SELECT `table1`.`col1`,`table1`.`col2`,@row:=@row+1
FROM `table1`
LEFT OUTER JOIN `table2` ON `table1`.`col1` = `table2`.`col5`
WHERE `table2`.`col5` IS NOT NULL
GROUP BY `col1` ORDER BY `table1`.`col7` DESC LIMIT 0,10

最佳答案

将带有 ORDER BY 子句的查询移动到子查询中。

SET @row = 0;

SELECT col1, col2, @row := @row+1
FROM (SELECT table1.col1, table1.col2
FROM table1
LEFT JOIN table2 ON table1.col1 = table2.col5
WHERE table2.col5 IS NOT NULL
GROUP BY col1
ORDER BY table1.col7 DESC
LIMIT 0, 10) AS subquery

关于php - 如何在选择查询中获取行号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com