SELECT *, COUNT(*) AS conteggio FROM (
(SELECT uno, ruota, data FROM table WHERE ruota = 'BA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT due, ruota, data FROM table WHERE ruota = 'BA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT tre, ruota, data FROM table WHERE ruota = 'BA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT quattro, ruota, data FROM table WHERE ruota = 'BA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT cinque, ruota, data FROM table WHERE ruota = 'BA'
ORDER BY data DESC LIMIT 540)
) t
GROUP BY uno
ORDER BY conteggio DESC LIMIT 20
-------------------------------------------------
SELECT *, COUNT(*) AS conteggio FROM (
(SELECT uno, ruota, data FROM table WHERE ruota = 'CA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT due, ruota, data FROM table WHERE ruota = 'CA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT tre, ruota, data FROM table WHERE ruota = 'CA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT quattro, ruota, data FROM table WHERE ruota = 'CA'
ORDER BY data DESC LIMIT 540)
UNION ALL
(SELECT cinque, ruota, data FROM table WHERE ruota = 'CA'
ORDER BY data DESC LIMIT 540)
) t
GROUP BY uno
ORDER BY conteggio DESC LIMIT 20
有没有办法只使用一个查询而不是上面的两个查询?
唯一的区别是WHERE ruota = 'VARIABLE'
答案 0 :(得分:3)
规范化您的数据:
CREATE TABLE newtable (
col INT,
INDEX (ruota, val),
FOREIGN KEY(ruota, data) REFERENCES `table` (ruota, data)
) SELECT 1, uno val, ruota, data FROM `table` UNION ALL
SELECT 2, due, ruota, data FROM `table` UNION ALL
SELECT 3, tre, ruota, data FROM `table` UNION ALL
SELECT 4, quattro, ruota, data FROM `table` UNION ALL
SELECT 5, cinque, ruota, data FROM `table`;
ALTER TABLE `table`
DROP COLUMN uno,
DROP COLUMN due,
DROP COLUMN tre,
DROP COLUMN quattro,
DROP COLUMN cinque;
简化您现有的查询:
SELECT val, ruota, LEAST(COUNT(*), 540) conteggio
FROM newtable
WHERE ruota = ?
GROUP BY val
ORDER BY conteggio DESC
LIMIT 20;
合并查询:
SELECT val, ruota, LEAST(COUNT(*), 540) conteggio
FROM newtable
WHERE ruota IN ('BA', 'CA')
GROUP BY ruota, val
ORDER BY conteggio DESC
LIMIT 20;
答案 1 :(得分:3)
写作时
WHERE ruota = 'CA' ORDER BY data DESC LIMIT 540
你问“给卡利亚里的最后540次提取”。添加这些来自巴里的那些,你得到1080行。
你要问,
WHERE ruota IN ('CA','BA') ORDER BY data DESC LIMIT 540
你只能获得540行,所以你必须首先将限制提高到1080 。
完成后,您确定您的存档已完成吗?如果不是,或者两个轮子的提取次数不同,那么你得到的1080行(CA + BA)与(CA)+(BA)不同。
此外,您不需要分组的提取日期。所以你可以查询:
SELECT ruota, numero, COUNT(*) AS conteggio FROM (
(SELECT uno AS numero, ruota FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080)
UNION ALL
(SELECT due AS numero, ruota FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080)
... ) AS preliminare
GROUP BY ruota, numero;
如果您只对数字频率感兴趣,则不需要方向盘,所以:
SELECT numero, COUNT(*) AS conteggio FROM (
(SELECT uno AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080)
UNION ALL
(SELECT due AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080)
... ) AS preliminare
GROUP BY numero;
最后,您可以通过分组子选择和总计部分计数进行试验,以防它可能更快:
SELECT ruota, numero, SUM(conteggio) AS conteggio FROM (
SELECT ruota, numero, count(numero) AS conteggio FROM (
SELECT ruota, uno AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080 ) AS primo GROUP BY ruota, numero
UNION ALL SELECT ruota, numero, count(numero) AS conteggio FROM (
SELECT ruota, due AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080 ) AS secondo GROUP BY ruota, numero
UNION ALL SELECT ruota, numero, count(numero) AS conteggio FROM (
SELECT ruota, tre AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080 ) AS terzo GROUP BY ruota, numero
UNION ALL SELECT ruota, numero, count(numero) AS conteggio FROM (
SELECT ruota, quattro AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080 ) AS quarto GROUP BY ruota, numero
UNION ALL SELECT ruota, numero, count(numero) AS conteggio FROM (
SELECT ruota, cinque AS numero FROM estrazioni WHERE ruota IN ('CA','BA')
ORDER BY data DESC LIMIT 1080 ) AS quinto GROUP BY ruota, numero
) AS preliminare GROUP BY ruota, numero ORDER BY conteggio DESC LIMIT 20;
最后一个版本对五个1080行子选择中的出现进行求和,然后将它们从五行选择中求和,将其分组五次1080而不是一次分组5400行。它应该使用更少的内存;是否更快,我不确定。
更新:我修复了SQL并验证了最后一次查询。
答案 2 :(得分:0)
您可以在SQL中使用IN
子句,并将这两个变量指定为逗号分隔值,并在同一查询中选择结果,而不是再次重复完整的SQL脚本。
像
WHERE ruota IN ('CA' ,'BA')