我有下表包含6/49彩票的中奖号码.
+-----+------------+----+----+----+----+----+----+-------+ | id | draw | n1 | n2 | n3 | n4 | n5 | n6 | bonus | +-----+------------+----+----+----+----+----+----+-------+ | 1 | 1982-06-12 | 3 | 11 | 12 | 14 | 41 | 43 | 13 | | 2 | 1982-06-19 | 8 | 33 | 36 | 37 | 39 | 41 | 9 | | 3 | 1982-06-26 | 1 | 6 | 23 | 24 | 27 | 39 | 34 | | 4 | 1982-07-03 | 3 | 9 | 10 | 13 | 20 | 43 | 34 | | 5 | 1982-07-10 | 5 | 14 | 21 | 31 | 34 | 47 | 45 | | 6 | 1982-07-17 | 8 | 20 | 21 | 25 | 31 | 41 | 33 | | 7 | 1982-07-24 | 18 | 25 | 28 | 33 | 36 | 42 | 7 | | 8 | 1982-07-31 | 7 | 16 | 17 | 31 | 40 | 48 | 26 | | 9 | 1982-08-07 | 5 | 10 | 23 | 27 | 37 | 38 | 33 | | 10 | 1982-08-14 | 4 | 15 | 30 | 37 | 46 | 48 | 3 | +-----+------------+----+----+----+----+----+----+-------+
我想创建一个频率图表来查看n1,n2,...,n6列中数字的出现.
SELECT n1, COUNT(n1) as freq1 FROM lottery GROUP BY n1; SELECT n2, COUNT(n2) as freq2 FROM lottery GROUP BY n2; SELECT n3, COUNT(n3) as freq3 FROM lottery GROUP BY n3; SELECT n4, COUNT(n4) as freq4 FROM lottery GROUP BY n4; SELECT n5, COUNT(n5) as freq5 FROM lottery GROUP BY n5; SELECT n6, COUNT(n6) as freq6 FROM lottery GROUP BY n6;
基本上我正在尝试将上述查询合并到一个查询中.
提前致谢.
根据您的表布局,您不能通过任何其他方式将查询组合到一个查询中,而不是将查询联合起来,或者将基表联合到转置版本中:
SELECT n, COUNT(*) as freq FROM ( SELECT n1 AS n FROM lottery UNION ALL SELECT n2 FROM lottery UNION ALL SELECT n3 FROM lottery UNION ALL SELECT n4 FROM lottery UNION ALL SELECT n5 FROM lottery UNION ALL SELECT n6 FROM lottery ) as transposed GROUP BY n ORDER BY COUNT(*) DESC