我學習MySQL手冊: http://dev.mysql.com/doc/refman/5.6/en/example-maximum-column-group-row.html 和我很困惑與「where.s1.article = s2.article」的一部分,這是什麼意思MySQL的子查詢,其中和group by
Task: For each article, find the dealer or dealers with the most expensive price.
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | A | 3.45 |
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | B | 1.45 |
| 0003 | C | 1.69 |
| 0003 | D | 1.25 |
| 0004 | D | 19.95 |
+---------+--------+-------+
SELECT article, dealer, price
FROM shop s1
WHERE price=(SELECT MAX(s2.price)
FROM shop s2
WHERE s1.article = s2.article);
+---------+--------+-------+
| article | dealer | price |
+---------+--------+-------+
| 0001 | B | 3.99 |
| 0002 | A | 10.99 |
| 0003 | C | 1.69 |
| 0004 | D | 19.95 |
+---------+--------+-------+
?
我試着寫類似的代碼:
SELECT sex, name, birthday
From user
Where birthday=(SELECT MAX(birthday)
FROM user
group by sex);
但我的輸出錯誤:子查詢返回超過1行,爲什麼
我不確定,我可能是錯的,但我認爲這是因爲'subquery返回多於一行'。 – Sebas 2013-02-28 21:11:34