在80多个表上找到最大值的有效方法

时间:2017-08-22 11:01:22

标签: mysql

我有80个表,我想过滤80个表中价格最高的人。我打算使用这个查询:

SELECT id
 FROM (SELECT id, price FROM T1 WHERE price = (SELECT MAX(price) FROM T1)
    UNION
    SELECT id, price FROM T2 WHERE price = (SELECT MAX(price) FROM T2)
    UNION
    SELECT id, price FROM T3 WHERE price = (SELECT MAX(price) FROM T3)
   ) AS M
 ORDER BY price DESC
 LIMIT 1

但我发现它效率低下。还有其他方法吗?先谢谢

1 个答案:

答案 0 :(得分:1)

我认为这会快得多。

SELECT id, price FROM t1
UNION SELECT id, price FROM t2
UNION SELECT id, price FROM t3
ORDER BY price DESC 
LIMIT 1