0
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)

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

4

3 回答 3

1

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

CREATE TABLE unified_table (
  product_type,
  price,
  model
)

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

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

但是,如果您不能或不愿相信优化器来处理 UNION 的后果......

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
于 2012-07-06T13:59:44.807 回答
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
于 2012-07-06T13:46:33.133 回答
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 个值要比较,来自不同的表,没有关系,你必须做一个联合,然后比较它们

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

于 2012-07-06T13:48:04.863 回答