从 SELECT 查询中获取最大值的记录
Get record with Max value from SELECT query
我正在尝试 get max value
我的查询中的一列,但我找不到任何解决方案。我的查询是
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id
然后我收到一些如下的记录
ID price
-----------
1 95000.0
6 160000.0
10 150000.0
3 137500.0
5 122500.0
4 195000.0
2 195000.0
7 130000.0
8 205000.0
9 130000.0
11 205000.0
我想得到价格max value
的record
,分别是:
ID price
------------
8 205000.0
11 205000.0
我在 MySQL 服务器上使用 Navicat
更新:
我确实尝试使用:
SELECT pz.id, MAX(sz.Price + cr.AddedCost) AS price ....
但是没有成功,因为返回的 id 是错误的
使用 Max 函数 :) select MAX(column) from ...
select MAX(col) 来自哪里
但请注意,此 col 必须是数字而不是字符串
试试这个:
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz,
(
select max(sz.Price + cr.AddedCost) as v
from crust cr, size sz
where pz.SizeID = sz.id
and pz.CrustID = tp.id
)t
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id AND
(sz.Price + cr.AddedCost) = t.v
当你这样做时会发生什么。
将所有旧式逗号分隔连接更改为内部连接以提高可读性。
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz
INNER JOIN crust cr
ON pz.CrustID = cr.id
INNER JOIN size sz
ON pz.SizeID = sz.id
WHERE sz.Price + cr.AddedCost = (SELECT sz.Price + cr.AddedCost AS price
FROM piz pz1
INNER JOIN crust cr1
ON pz1.CrustID = cr1.id
INNER JOIN size sz1
ON pz1.SizeID = sz1.id
ORDER BY price DESC limit 1 )
试试这个:
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id
HAVING
price = (SELECT max(sz.Price + cr.AddedCost) from crust cr, size sz)
我正在尝试 get max value
我的查询中的一列,但我找不到任何解决方案。我的查询是
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id
然后我收到一些如下的记录
ID price
-----------
1 95000.0
6 160000.0
10 150000.0
3 137500.0
5 122500.0
4 195000.0
2 195000.0
7 130000.0
8 205000.0
9 130000.0
11 205000.0
我想得到价格max value
的record
,分别是:
ID price
------------
8 205000.0
11 205000.0
我在 MySQL 服务器上使用 Navicat
更新: 我确实尝试使用:
SELECT pz.id, MAX(sz.Price + cr.AddedCost) AS price ....
但是没有成功,因为返回的 id 是错误的
使用 Max 函数 :) select MAX(column) from ...
select MAX(col) 来自哪里 但请注意,此 col 必须是数字而不是字符串
试试这个:
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz,
(
select max(sz.Price + cr.AddedCost) as v
from crust cr, size sz
where pz.SizeID = sz.id
and pz.CrustID = tp.id
)t
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id AND
(sz.Price + cr.AddedCost) = t.v
当你这样做时会发生什么。
将所有旧式逗号分隔连接更改为内部连接以提高可读性。
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz
INNER JOIN crust cr
ON pz.CrustID = cr.id
INNER JOIN size sz
ON pz.SizeID = sz.id
WHERE sz.Price + cr.AddedCost = (SELECT sz.Price + cr.AddedCost AS price
FROM piz pz1
INNER JOIN crust cr1
ON pz1.CrustID = cr1.id
INNER JOIN size sz1
ON pz1.SizeID = sz1.id
ORDER BY price DESC limit 1 )
试试这个:
SELECT pz.id,
sz.Price + cr.AddedCost AS price
FROM piz pz, crust cr, size sz
WHERE
pz.SizeID = sz.id AND
pz.CrustID = tp.id
HAVING
price = (SELECT max(sz.Price + cr.AddedCost) from crust cr, size sz)