这将显示所有具有最高价格或最低价格的产品:
SELECT Products.*
FROM Products
WHERE Price = (SELECT MAX(Price) FROM Products)
OR Price = (SELECT MIN(Price) FROM Products)
或者,也许你想要这样的东西:
SELECT
Products.*,
m.mx As Highest,
m.mn As Lowest,
CASE WHEN Products.Price = m.mx THEN 'Max' ELSE 'Min' END As Is_Max_or_Min
FROM
Products INNER JOIN (
SELECT MAX(Price) mx, MIN(Price) mn
FROM Products
) m ON Products.Price IN (m.mx, m.mn)
如果您希望它们位于同一行,并且只有一种产品具有最高价格,而只有一种产品具有最低价格,则可以使用以下内容:
SELECT
m.Lowest, p1.Name Name_Lowest,
m.Highest, p2.Name Name_Highest
FROM
(SELECT MIN(Price) Lowest, MAX(Price) Highest FROM Products) m
INNER JOIN Products p1 ON m.Lowest = p1.Price
INNER JOIN Products p2 ON m.Highest = p2.Price
LIMIT 1
或者,如果您只需要更简单的东西,您可以使用这个:
(SELECT 'Max' Is_Max_Or_Min, Products.*
FROM Products ORDER BY Price DESC LIMIT 1)
UNION ALL
(SELECT 'Min', Products.*
FROM Products ORDER BY Price LIMIT 1)