SQL查询简化

时间:2012-07-06 13:36:37

标签: sql simplify simplification

select model from (
    select price, model from pc where price = (select max(price) from pc)
    union
    select price, model from laptop where price = (select max(price) from laptop)
    union
    select price, model from printer where price = (select max(price) from printer)
) t1 where price = (select max(price) from (
    select price, model from pc where price = (select max(price) from pc)
    union
    select price, model from laptop where price = (select max(price) from laptop)
    union
    select price, model from printer where price = (select max(price) from printer)
) t2 )

我对SQL很新,所以我的问题非常简单,但我想澄清一点。我是对的,这个查询不能简化为这样的东西吗?

select model from (
    select price, model from pc where price = (select max(price) from pc)
    union
    select price, model from laptop where price = (select max(price) from laptop)
    union
    select price, model from printer where price = (select max(price) from printer)
) t1 where price = (select max(price) from t1)

如果它不可能,我们运行两个相同的子查询是不是一件坏事?

3 个答案:

答案 0 :(得分:1)

我仍然说要用一张桌子,这是最佳实践设计。 (不会不必要地复制相同的表格。)

CREATE TABLE unified_table (
  product_type,
  price,
  model
)

这样做可启用此查询...

SELECT
  *
FROM
  unified_table
WHERE
  price = (SELECT MAX(price) FROM unified_table)

但是,如果你不能或不愿意相信优化者来处理联盟的后果......

SELECT
  *
FROM
(
  SELECT * FROM pc
  UNION ALL
  SELECT * FROM laptop
  UNION ALL
  SELECT * FROM printer
) t1
WHERE
  price = (SELECT MAX(price) FROM (SELECT price FROM pc
                                   UNION ALL
                                   SELECT price FROM laptop
                                   UNION ALL
                                   SELECT price FROM printer
                                  ) t2
          )

优化工具将了解如何对其进行优化,以便删除多余的搜索。


修改

作为妥协,您可以制作统一的视图,并查询...

CREATE VIEW unified_table AS
  SELECT 'pc'      AS type, * FROM pc
  UNION ALL
  SELECT 'laptop'  AS type, * FROM laptop
  UNION ALL
  SELECT 'printer' AS type, * FROM printer

答案 1 :(得分:0)

尝试这样的事情:

select model, price
from (
    select price, model from pc order by price desc limit 1
    union
    select price, model from laptop order by price desc limit 1
    union
    select price, model from printer order by price desc limit 1
) t1 
order by price desc
limit 1

但是我建议你检查一下你的数据库结构,这似乎就是你根据类型为同一个东西(项目)创建了多个表。您可以将所有这些保留在一个表中,仅通过类型列的内容进行区分。

无限制:

select t1.model, t1.price
from 
(select max(price) p
 from
    select max(price) p from pc
    union
    select max(price) p from laptop
    union
    select max(price) p from printer
) max_price
JOIN (
    select price, model from pc
    union
    select price, model from laptop
    union
    select price, model from printer
) t1 ON price >= max_price.p

答案 2 :(得分:0)

select * from (
    select price, model from pc where price = (select max(price) from pc)
    union
    select price, model from laptop where price = (select max(price) from laptop)
    union
    select price, model from printer where price = (select max(price) from printer)
) order by Price desc limit 1

由于你有3个值要比较,从不同的表中,没有关系,你必须做一个联盟,然后比较它们

这是一种方式,您无需再次计算价格。