gpt4 book ai didi

mysql - MySQL显示rownum问题

转载 作者:可可西里 更新时间:2023-11-01 08:20:54 24 4
gpt4 key购买 nike

我有这个查询:

SET @row_num = 0;
SELECT
(SELECT @row_num := @row_num + 1) AS itempurchase_code,
(SELECT supplier_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY supplier_code ORDER BY COUNT(*) DESC LIMIT 1) AS supplier_code,
(SELECT user_code FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY user_code ORDER BY COUNT(*) DESC LIMIT 1) AS user_code,
22 AS status_code,
item_invoicecodesupplier AS item_invoicecodesupplier,
(SELECT itempurchase_date FROM qa_items_purchases a WHERE a.item_invoicecodesupplier = b.item_invoicecodesupplier GROUP BY itempurchase_date ORDER BY COUNT(*) DESC LIMIT 1) AS itempurchase_date
FROM qa_items_purchases b
GROUP BY (item_invoicecodesupplier)
ORDER BY itempurchase_code;

我得到这个结果: enter image description here

如果您看到没有 (2) itempurchase_code 列,我该怎么做才能按顺序显示数字?

最佳答案

如果您的查询包含 GROUP BY,则必须在外部查询中添加行号。

SET @row_num = 0;
SELECT (SELECT @row_num := @row_num + 1) AS itempurchase_code, *
FROM
(
SELECT ... -- your original query goes here
) AS T1
ORDER BY itempurchase_code

关于mysql - MySQL显示rownum问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11460300/

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