2011-02-11 75 views
1

我試圖從數據庫中選擇非常具體的數據,但我的查詢看起來不起作用。這個查詢是否正確?PHP/MySQL查詢。選擇一個或另一個

SELECT * 
FROM matches 
WHERE compo_id = '1' 
    AND match_id < '5' 
    AND clan_user_id_1 = '0' 
    OR clan_user_id_2 = '0' 

所以我想選擇其中任一clan_user_id_1或clan_user_id_2等於零的所有比賽。

回答

5

把它括在括號中,這是否工作?

SELECT * FROM matches WHERE compo_id = '1' 
AND match_id < '5' 
AND (clan_user_id_1 = '0' OR clan_user_id_2 = '0') 
2
SELECT 
    * 
FROM 
    matches 
WHERE 
    compo_id = 1 
    AND 
    match_id < 5 
    AND 
    (
    clan_user_id_1 = 0 
     OR 
    clan_user_id_2 = 0 
) 

而且,除非你真的需要的一切,它平時最好不要SELECT *。明確定義您需要選擇哪些字段。

2

試試這個

SELECT * FROM matches WHERE compo_id = '1' AND match_id < '5' AND (clan_user_id_1 = '0' OR clan_user_id_2 = '0') 
1

當混合了AND和OR這樣的條款,你必須指定支架,使其明確清楚自己想要什麼。

你有

A and B and C or D 

可以得到解釋不同的方式:

A and B and (C or D) 

(A and B and C) or D