2012-02-08 118 views
2

我也有一個關於不同和/或group by的問題。我如下表:使用不同和/或通過篩選篩選選擇

| art | ean    | obs | vke | 
--------------------------------------------------- 
| type | 123456789| 1 | 100 | 
| type | 123456789| 0 | 50 | 
| type | 123456789| 0 | 60 | 
| type | 123456789| 0 | 70 | 

我需要查詢與實測值= 1,總是選擇該行只便宜別人與實測值= 0。所有其它的等於EAN不應該在所有上市。這可能嗎 ?

所以結果應該是:

| type | 123456789| 1 | 100 | 
| type | 123456789| 0 | 50 | 

回答

1
select art, ean, obs, vke 
from table_name 
where obs = 1 
union all 
select art, ean, obs, min(vke) as vke 
from table_name 
where obs = 0 
group by art, ean, obs 

我倒是union兩個結果第一個與obs aquals 1和第二的所有行的所有行通過art分組,eanobsobs等號0min的值爲vke

查看更多about uniongroup by

1
SELECT art, ean, obs, vke 
FROM table_name 
WHERE obs = 1 
UNION ALL 
(
    SELECT art, ean, obs, vke 
    FROM table_name 
    WHERE obs = 0 
    ORDER BY vke 
    LIMIT 1 
) 
+0

你好 - 再次類似的問題: – 2012-05-22 10:47:18