Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 使用Max在我的SQL中查找最高价格_Mysql_Sql - Fatal编程技术网

Mysql 使用Max在我的SQL中查找最高价格

Mysql 使用Max在我的SQL中查找最高价格,mysql,sql,Mysql,Sql,我想找价格最高的彩色打印机制造商?我的输出应该列出制造商、打印机型号和价格。我假设没有两种型号的最高价格相同 我尝试过:从打印机中选择型号、类型、最大(价格),其中color=“TRUE”按型号分组 但它返回两个价格,而不是一个。我也不知道如何加入表格,以显示制造商的最高价格 这是我的模式: Product (maker, model, type) Desktop (model, speed, ram, hd, price) Laptop (model, speed, ram, hd, scr

我想找价格最高的彩色打印机制造商?我的输出应该列出制造商、打印机型号和价格。我假设没有两种型号的最高价格相同

我尝试过:
从打印机中选择型号、类型、最大(价格),其中color=“TRUE”按型号分组

但它返回两个价格,而不是一个。我也不知道如何加入表格,以显示制造商的最高价格

这是我的模式:

Product (maker, model, type)
Desktop (model, speed, ram, hd, price)
Laptop  (model, speed, ram, hd, screen, price)
Printer (model, color, type, price)

Products
Maker   Model   Type
A   1001    desktop
A   1002    desktop
A   1003    desktop
B   1004    desktop
B   1006    desktop
B   3002    printer
B   3004    printer
C   1005    desktop
C   1007    desktop
D   1008    desktop
D   1009    desktop
D   1010    desktop
D   2001    laptop
D   2002    laptop
D   2003    laptop
D   3001    printer
D   3003    printer
E   2004    laptop
E   2008    laptop
F   2005    laptop
G   2006    laptop
G   2007    laptop
H   3005    printer
I   3006    printer

Desktop
Model   Speed   Ram HD  Price
1001    2.5 256 80  595
1002    2.0 256 80  399
1003    3.1 512 120 899
1004    3.1 1024    120 999
1005    3.1 256 100 999
1006    4.5 512 180 1099
1007    4.5 512 200 1399
1008    4.0 512 100 1199
1009    4.5 512 120 1299
1010    3.0 256 60  495

Laptop
Model   Speed   Ram HD  Screen  price
2001    1.8 256 30  12  799
2002    2.2 128 20  14  1499
2003    2.2 512 40  14  1699
2004    2.5 256 40  12  1499
2005    2.5 512 60  15  1799
2006    2.3 256 40  15  999
2007    3.0 1024    80  17  1899
2008    2.3 256 30  14  1599

Printer
Model   Color   Type    price
3001    True    InkJet  175
3002    True    InkJet  150
3003    False   Laser   295
3004    False   Laser   325
3005    False   inkjet  80
3006    False   Laser   259

在下面的查询中,我使用子查询在
打印机
表中查找最高的打印机价格。这与彩色打印一起作为
WHERE
条款中的要求使用

SELECT p1.Maker,
       p2.model,
       p2.type,
       p2.price
FROM Products p1
INNER JOIN Printer p2
    ON p1.model = p2.model
WHERE p2.price = (SELECT MAX(price) FROM Printer WHERE color = "TRUE")

要返回单一价格,可以使用“限制”

select model, type, max(Price) from Printer where color="TRUE" group by model limit 1.

这很有效。我们是否可以显示最高价格和型号?目前,它只显示MakerEverything的工作方式,除了p2.color=“TRUE”。当我在底部输入它时,它返回一个空集,此时应该有一个最大值175@coder很抱歉,颜色检查应该在查找最高价格的子查询中,而不是外部
WHERE
子句。这将返回所有价格等于最昂贵的彩色打印机的打印机。一个非常奇怪的请求。我编辑以重新包含样本数据。如果没有这个,恐怕你的问题以后会被否决。
select model, type, max(Price) from Printer where color="TRUE" group by model limit 1.