【问题标题】:MySQl cheapest color printerMySQl 最便宜的彩色打印机
【发布时间】:2012-04-05 15:59:59
【问题描述】:

关系:

  • 产品(制造商、型号、类型)
  • 笔记本电脑(型号、价格、速度、内存、高清、屏幕)
  • PC(型号、价格、速度、内存、高清)
  • 打印机(型号、价格、颜色、价格)

我现在正在寻找最便宜的彩色打印机的制造商

我的查询:

SELECT maker FROM 
(SELECT model FROM printer NATURAL JOIN product WHERE printer.color = '1') AS t1  
WHERE price < all;

【问题讨论】:

  • 你不能只按价格 (asc) 排序,并将结果数限制为 1 个吗?
  • 打印机关系中没有制造商。我需要加入产品和打印机,然后找到价格最低的制造商。
  • 您不需要子查询。加入就足够了。

标签: mysql


【解决方案1】:

我是这样解决的:

select distinct Pro.maker, Pri.price
from Product Pro join Printer Pri on Pro.model = Pri.model
where Pri.price = (select min(price) from Printer where color = 'y')  and Pri.color = 'y'

在子查询(select min(price) from Printer where color = 'y') 中,我们应该指定只搜索彩色打印机的价格。

我不喜欢这个解决方案是我们必须对彩色打印机进行两次过滤:

  • 在子查询中(select min(price) from Printer where color = 'y')
  • 在顶部查询... and Pri.color = 'y'

虽然我不知道如何摆脱在两个地方指定这个。

【讨论】:

    【解决方案2】:
        SELECT `p.maker`
          FROM `product` AS `p`
    INNER JOIN printer as pr
         WHERE p.model = pr.model
      ORDER BY pr.price ASC LIMIT 1
    

    【讨论】:

    • 打印机关系中没有制造商。
    • 它在子查询中。一旦自然连接将两个关系连接起来,那么该关系(子查询)将在其关系中包含一个制造商。
    • 如果有 2 台打印机价格最便宜,但价格相同怎么办?
    【解决方案3】:

    使用子查询你应该这样解决:

    select pro.maker from printer pri
    natural join product pro
    where pri.color = '1'
      and pri.price <= all (select price from printer where pri.color = '1')
    

    带有LIMIT 子句:

    select pro.maker from printer pri
    natural join product pro
    where pri.color = '1'
    order by pri.price
    limit 1
    

    【讨论】:

    • 此条件pri.price &lt;= all (select price from printer) 将要求所有打印机的价格最低,而不仅仅是彩色打印机。考虑到我们在查询中有where pri.color = '1',结果集将为空,因为pri.price &lt;= all (select price from printer) 将给出比彩色便宜的最便宜的黑白打印机的价格,而没有彩色打印机将是以这样的价格找到。
    • 关于第二个解决方案:如果有 2 台价格相同的不同打印机,结果却是最便宜的怎么办?
    • 修复了第一个查询。第二个不一定是错误的,因为 OP 特别要求“最便宜的彩色打印机的制造商”(注意他期待一个)。即使问题是错误的,那么第一个解决方案也应该有效。
    【解决方案4】:

    在 Mysql 中以下查询也有效:

    select b.maker, min(a.price) from printer a, product b 
    where a.color = '1'
    and a.model = b.model;
    

    【讨论】:

    • 打印机关系中没有制造商。
    • 啊,好吧,我只是看了最旧的答案 :) 我会在几分钟内修复它。
    • 我不知道我可以这样做。我一直在尝试引用查询中的特定表时遇到初学者问题。我不知道我可以实例化(拼写...)一个表的名称,以便可以引用它。我是否也可以像您在这里所说的那样,在我命名之前引用我命名的关系(打印机 a),我接受它吗?
    • 是的,当然你可以像这样引用关系别名。您可以尝试将上述查询直接运行到 MySQL 客户端中,看看它是否能得到您想要的结果。
    【解决方案5】:
    select distinct product.maker, price 
    from printer
    join product
    on ( product.model=printer.model)
    where color='y' and
    price = (select min(price) from printer where color='y')
    

    【讨论】:

      【解决方案6】:
      SELECT DISTINCT maker, price 
      FROM Printer 
      JOIN Product 
      ON Product.model=Printer.model 
      WHERE Printer.color=1 
      AND Printer.price=
      (SELECT MIN(price) 
      FROM Printer
      WHERE color=1)
      

      没错。

      您的查询结果:

      maker      price
      
      D          270.0000
      

      【讨论】:

        【解决方案7】:

        尝试以下查询:

        Select distinct maker,price from Product 
        Join Printer on printer.color='y'and Product.model= Printer.model
        group by Printer.model
        having price = (select min(price) from Printer where color='y')
        

        【讨论】:

          【解决方案8】:

          我是初学者,但我试过这样

          select distinct p.maker, pr.price from
          
          (select maker, model from product)p
          
          inner join 
          
          (select model, price from printer where color='y' and price=(select min(price) from 
          printer where color='y'))pr
          
          on p.model=pr.model
          

          【讨论】:

            【解决方案9】:
            select ‍product.maker, printer.price
            from product join printer on product.model=printer.model
            where price= (select min(price) from printer where color='y')
            

            【讨论】:

              【解决方案10】:

              可以有多种解决方案,下面也可以。

              你可以试试这个,

              select  top 1 t1.maker, t2.price from  product t1, printer t2
              where t2.color = 'y'
              and t1.model = t2.model order by t2.price
              

              【讨论】:

                猜你喜欢
                • 2013-01-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-09-10
                • 2021-05-06
                • 1970-01-01
                • 2019-06-25
                • 2016-02-19
                相关资源
                最近更新 更多