关系:
- 产品(制造商、型号、类型)
- 笔记本电脑(型号、价格、速度、内存、高清、屏幕)
- PC(型号,价格,速度,内存,高清)
- 打印机(型号、价格、颜色、价格)
我现在正在寻找最便宜的彩色打印机的制造商
我的查询:
SELECT maker FROM
(SELECT model FROM printer NATURAL JOIN product WHERE printer.color = '1') AS t1
WHERE price < all;
关系:
我现在正在寻找最便宜的彩色打印机的制造商
我的查询:
SELECT maker FROM
(SELECT model FROM printer NATURAL JOIN product WHERE printer.color = '1') AS t1
WHERE price < all;
我是这样解决的:
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'
虽然我不知道如何摆脱在这两个地方指定这一点。
使用子查询,您应该这样解决:
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
SELECT `p.maker`
FROM `product` AS `p`
INNER JOIN printer as pr
WHERE p.model = pr.model
ORDER BY pr.price ASC LIMIT 1
在 Mysql 中,以下查询也有效:
select b.maker, min(a.price) from printer a, product b
where a.color = '1'
and a.model = b.model;
尝试以下查询:
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')
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')
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
我是初学者,但我尝试过这样
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
select product.maker, printer.price
from product join printer on product.model=printer.model
where price= (select min(price) from printer where color='y')
可以有多种解决方案,下面也可以。
你可以试试这个,
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