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'