考虑以下两个表:
系列
id name screen_name deleted_at
9 Series Name 9 Screen Name 9 NULL
11 Series Oneb seriesoneb NULL
15 Mad Dogs maddogs NULL
票
id series_id subject deleted_at
8 15 Subject 8 NULL
13 11 Subject 13 NULL
28 9 Subject 28 NULL
34 11 Subject 34 NULL
37 9 Subject 37 NULL
41 9 Subject 41 NULL
48 9 Subject 48 NULL
以下查询怎么可能:
SELECT *
FROM `tickets`
WHERE (
SELECT count( * )
FROM `series`
WHERE `series`.`deleted_at` IS NULL
AND `tickets`.`series_id` = `series`.`id`
AND screen_name LIKE '%series name%'
OR name LIKE '%series name%'
AND `series`.`deleted_at` IS NULL
)
给我 7行,不应该 6 吗?
答案 0 :(得分:3)
尝试为OR
条件添加括号,如下所示
SELECT *
FROM `tickets`
WHERE (
SELECT count( * )
FROM `series`
WHERE `series`.`deleted_at` IS NULL
AND `tickets`.`series_id` = `series`.`id`
AND (screen_name LIKE '%series name%' OR name LIKE '%series name%')
AND `series`.`deleted_at` IS NULL
)
答案 1 :(得分:2)
您的问题可能是where
子句中的优先级。它被解析为:
WHERE (`series`.`deleted_at` IS NULL AND
`tickets`.`series_id` = `series`.`id` AND
screen_name LIKE '%series name%'
) OR
(name LIKE '%series name%' AND
`series`.`deleted_at` IS NULL
)
我怀疑你想要:
WHERE `series`.`deleted_at` IS NULL AND
`tickets`.`series_id` = `series`.`id` AND
(screen_name LIKE '%series name%' OR
name LIKE '%series name%'
)