我有如下表:古怪的行爲並具有MySQL時,最大== 0
mysql> select * from foo;
| id | value | bar |
+----+-------+------+
| 1 | 2 | 3 |
| 2 | 0 | 3 |
| 1 | 1 | 5 |
我想最大value
每個id
選擇元組。但是,當max(value)
爲0時,我沒有得到結果。
mysql> select id,max(value),bar from foo group by id having max(value);
| id | max(value) | bar |
+----+------------+------+
| 1 | 2 | 3 |
這應該是這樣的行爲,如果是這樣,爲什麼?
'select id,max(value)from foo group by id'就足以讓每個id獲得最大值。你不需要'having子句'。 –
已將max(value)翻譯爲真或非0.您可能想要從t中獲得max(value)= SELECT max(value) – Mihai
除了'having'的行爲並不像您想象的那樣,讓我獲得最大價值*的記錄。它會檢查所使用的謂詞是否爲真。在你的情況下,謂詞很簡單,在第一種情況下,'max(value)'等於'0'。這在MySQL中被認爲是錯誤的,所以沒有任何返回。 –