一尘不染

显示行数问题MySQL

sql

我有这个查询:

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;

我得到这个结果: 在此处输入图片说明

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


阅读 209

收藏
2021-03-08

共1个答案

一尘不染

如果查询包含,则必须在外部查询中添加行号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
2021-03-08