Mysql 查询结果为每个类别生成月份(1-12)并按类别排列在同一列中
Mysql query result generate months(1-12) for each category and arrange in same column order by category
我想为每个类别生成 1-12 的月号。有两个类别,DEBIT 和 CREDIT。所以,在完成后打印 1-12 作为 DEBIT。然后,它应该转到下一个类别并在类别列中使用 CREDIT 打印相同的内容。
SELECT mon, @c:=@c+1 as cat_no, category
FROM
(
SELECT @m:=@m+1 as mon FROM
( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12)a,
(SELECT @m:=0)c
)d,
(SELECT 'DEBIT' as category UNION SELECT 'CREDIT' as category)b,
(SELECT @c:=0)e
fiddle here.
结果:
结果显示列类别在转到下一个之前每个月显示两个类别。但是,我希望在下个月之前输出所有 1-12。
预计:
谢谢。
SELECT mon.mon, cat.cat_no, cat.category
FROM ( SELECT 1 mon UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 ) AS mon
CROSS JOIN ( SELECT 1 cat_no, 'DEBIT' category UNION
SELECT 2, 'CREDIT' ) cat
ORDER BY cat_no, mon
https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=60416e7875ba7eb886e804c0bddbcadb
我想为每个类别生成 1-12 的月号。有两个类别,DEBIT 和 CREDIT。所以,在完成后打印 1-12 作为 DEBIT。然后,它应该转到下一个类别并在类别列中使用 CREDIT 打印相同的内容。
SELECT mon, @c:=@c+1 as cat_no, category
FROM
(
SELECT @m:=@m+1 as mon FROM
( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION
SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12)a,
(SELECT @m:=0)c
)d,
(SELECT 'DEBIT' as category UNION SELECT 'CREDIT' as category)b,
(SELECT @c:=0)e
fiddle here.
结果:
结果显示列类别在转到下一个之前每个月显示两个类别。但是,我希望在下个月之前输出所有 1-12。
预计:
谢谢。
SELECT mon.mon, cat.cat_no, cat.category
FROM ( SELECT 1 mon UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION
SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION
SELECT 9 UNION SELECT 10 UNION SELECT 11 UNION SELECT 12 ) AS mon
CROSS JOIN ( SELECT 1 cat_no, 'DEBIT' category UNION
SELECT 2, 'CREDIT' ) cat
ORDER BY cat_no, mon
https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=60416e7875ba7eb886e804c0bddbcadb