我试图从数据库中选择非常具体的数据,但我的查询看起来不起作用。这个查询是否正确?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等于零的所有比赛。
我试图从数据库中选择非常具体的数据,但我的查询看起来不起作用。这个查询是否正确?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等于零的所有比赛。
把它括在括号中,这是否工作?
SELECT * FROM matches WHERE compo_id = '1'
AND match_id < '5'
AND (clan_user_id_1 = '0' OR clan_user_id_2 = '0')
SELECT
*
FROM
matches
WHERE
compo_id = 1
AND
match_id < 5
AND
(
clan_user_id_1 = 0
OR
clan_user_id_2 = 0
)
而且,除非你真的需要的一切,它平时最好不要SELECT *。明确定义您需要选择哪些字段。
试试这个
SELECT * FROM matches WHERE compo_id = '1' AND match_id < '5' AND (clan_user_id_1 = '0' OR clan_user_id_2 = '0')
当混合了AND和OR这样的条款,你必须指定支架,使其明确清楚自己想要什么。
你有
A and B and C or D
可以得到解释不同的方式:
A and B and (C or D)
或
(A and B and C) or D