這應該是非常簡單我知道,但對於我的生活,我無法讓我的查詢做我需要的。MySQL組由與where子句計數大於1
我在尋找找到所有屬於ref
的特定狀態的行(paid
),但僅限於找到多於1行的行。
這是我的示例表:
+-----+----------+----------+-------+
| id | deleted | status | ref |
+-----+----------+----------+-------+
| 1 | 0 | pending | 10001 |
| 2 | 0 | paid | 10001 |
| 3 | 0 | paid | 10001 |
| 4 | 0 | paid | 10002 |
| 5 | 1 | pending | 10002 |
| 6 | 1 | paid | 10002 |
| 7 | 0 | pending | 10003 |
| 8 | 0 | paid | 10003 |
| 9 | 0 | paid | 10003 |
| 10 | 0 | paid | 10003 |
| 11 | 0 | pending | 10004 |
| 12 | 0 | paid | 10004 |
| 13 | 1 | pending | 10005 |
| 14 | 1 | paid | 10005 |
| 15 | 1 | paid | 10005 |
| 16 | 0 | paid | 10005 |
| 17 | 0 | pending | 10006 |
| 18 | 0 | paid | 10006 |
| 19 | 0 | paid | 10006 |
+-----+----------+----------+-------+
這是我的SQL:
SELECT * FROM `orders`
WHERE `deleted` = 0 AND `status` = 'paid'
GROUP BY SUBSTR(`ref`,0,5)
HAVING COUNT(*) > 1
ORDER BY `id` DESC
我需要把它匹配的SUBSTR由於ref
有時包含附加的數字。
的問題是,我的查詢返回這樣的:
+-----+----------+---------+-------+
| id | deleted | status | ref |
+-----+----------+---------+-------+
| 2 | 0 | paid | 10001 |
+-----+----------+---------+-------+
當我想它可以返回ref
小號10001
,10003
& 10006
。
任何人都可以幫我弄清楚我做錯了什麼嗎?
感謝
,我被8秒打.. :) – barsju 2012-03-21 11:02:19
感謝名單!感謝..我沒有包括從文件引用..這是什麼花了我8秒.. :) – barsju 2012-03-21 11:37:29