douxiaochun4964 2015-12-30 06:19
浏览 132
已采纳

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

i try this query to get row number in selected rows, but the output comes like 23,56,78,.... i need to get 1,2,3 to every selected row. please help me

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
  • 写回答

2条回答 默认 最新

  • dongzi1209 2015-12-30 06:31
    关注

    Move the query with the ORDER BY clause into a subquery.

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,