【发布时间】:2017-05-21 13:49:33
【问题描述】:
[强迫症前言 -1] 我知道这个问题已经被回答了至少十亿次,但问题是我无法为我想要获得的答案建模.我不是 SQL 专家,这是肯定的;我对 SELECT、UPDATE、DELETE、ecc 等经典命令很有信心。所以我要感谢任何愿意帮助我的人。
说了这么多,假设我有一张这样的桌子:
|----|--------|------------|----|----------|---------|---------|------|
| id | code | category | mq | weight | weave | price | show |
|----|--------|------------|----|----------|---------|---------|------|
| 1 | DT450R | carbon | 1 | 450 | plain | 90 | 1 |
| 2 | DT450R | carbon | 2 | 450 | plain | 40 | 1 |
| 3 | DT450R | carbon | 5 | 450 | plain | 75 | 1 |
| 4 | ZX300R | carbon | 1 | 300 | plain | 12 | 0 |
| 5 | ZX300R | carbon | 15 | 300 | plain | 128 | 1 |
| 6 | ZX300R | carbon | 30 | 300 | plain | 92 | 1 |
| 7 | PP120Q | carbon | 3 | 120 | twill | 28 | 1 |
| 8 | PP120Q | carbon | 7 | 120 | twill | 65 | 1 |
| 9 | PP120Q | carbon | 9 | 120 | twill | 49 | 1 |
我希望我的查询为每个代码选择仅最低价格的行:
| 2 | DT450R | carbon | 2 | 450 | plain | 40 | 1 |
| 4 | ZX300R | carbon | 1 | 300 | plain | 12 | 0 |
| 7 | PP120Q | carbon | 3 | 120 | twill | 28 | 1 |
第一次尝试(基于 MySQL documentation 中对 MIN() 的解释,或者至少基于我所理解的它):
$sql = 'SELECT code, weight, weave, MIN(price)
FROM products
WHERE category="carbon" AND show="1"
GROUP BY code
ORDER BY weight ASC';
第二次尝试(基于this answer 这里的 SO):
$sql = 'SELECT a.code, a.weight, a.price, a.weave
FROM products a
INNER JOIN
(
SELECT code, weight, MIN(price) AS minprice, weave
FROM products
GROUP BY code
)
b ON a.code = b.code AND a.weave = b.weave AND a.price = b.minprice AND AND a.weight = b.weight
WHERE category="carbon" AND show="1"
ORDER BY a.weight ASC';
第三次尝试(基于this other answer 这里的 SO):
$sql = 'SELECT code, weight, weave, price
FROM products
INNER JOIN
(
SELECT MIN(price) price, code, weight, weave
FROM products
GROUP BY code
)
AS MIN ON MIN.code = products.code AND MIN.weight = products.weight AND MIN.weave = products.weave
WHERE category="carbon" AND show="1"
ORDER BY a.weight ASC';
说这些尝试都没有产生预期的结果可能是没有用的;只有第三种方法输出一些东西,而其他两种方法返回0 matches。我知道在第 2 和第 3 方法中,我将查询嵌套到查询中,但我不知道为什么它们不起作用。
【问题讨论】:
-
家庭作业,但至少你在努力。第三次尝试是正确的路径,但是除了派生表中的代码和 min(price) 之外,您不需要任何东西,然后您可以将其加入原始表中的代码和 WHERE price = derived.price
-
其实第二次尝试最接近。但是从子查询和
ON中去掉weave和weight。 -
为什么你的结果包含
id = 4?您的查询显示AND show = "1",而不是该行有show = "0"。