You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL内连接查询未返回指定用户Jobs子集结果的问题求助

MySQL查询过滤问题:连接与条件逻辑错误导致结果不符合预期

我正在编写一个MySQL查询,希望从结果集中返回过滤后的结果,但连接未按预期工作。

初始查询(返回目标子集)

以下查询可以正确返回用户ID为77的未完成活跃任务,共14行数据:

select `jobs`.* from `jobs` 
where exists (
    select * from `job_users` 
    where `jobs`.`number` = `job_users`.`job_number` 
    and `user_id` = 77
) 
and `is_active` = 1 
and `is_complete` = 0 
order by `number` desc 
limit 50 
offset 0;

有问题的查询(返回全表数据)

为了在上述子集基础上进一步搜索,我编写了以下查询,但它返回的是整个jobs表的数据集,而非用户ID为77的任务子集:

select `jobs`.* from `jobs` 
inner join `clients` on `jobs`.`client_id` = `clients`.`id` 
inner join `job_comments` on `jobs`.`client_id` = `job_comments`.`id` 
where exists (
    select * from `job_users` 
    where `jobs`.`number` = `job_users`.`job_number` 
    and `user_id` = 77
) 
and `is_active` = 1 
and `is_complete` = 0 
and `clients`.`title` like '%se%' 
or `jobs`.`title` like '%se%' 
or `jobs`.`number` like '%se%'
or `job_comments`.`comment` like '%se%'
order by `number` desc 
limit 50 
offset 0;

EXPLAIN执行结果

执行EXPLAIN后的输出如下:

id | select_type        | table        | partitions | type   | possible_keys                                      | key                        | key_len | ref                           | rows  | filtered | Extra
1  | PRIMARY            | jobs         | NULL       | index  | jobs_client_id_index                               | PRIMARY                    | 8       | NULL                          | 20799 | 100.00   | Using where; Backward index scan
1  | PRIMARY            | clients      | NULL       | eq_ref | PRIMARY                                            | PRIMARY                    | 8       | laravel_ja_dec.jobs.client_id | 1     | 100.00   | NULL
1  | PRIMARY            | job_comments | NULL       | eq_ref | PRIMARY                                            | PRIMARY                    | 8       | laravel_ja_dec.jobs.client_id | 1     | 100.00   | Using where
2  | DEPENDENT SUBQUERY | job_users    | NULL       | ref    | job_users_job_number_index,job_users_user_id_index | job_users_job_number_index | 8       | laravel_ja_dec.jobs.number    | 1     | 2.78     | Using where

我尝试调整连接顺序,但未解决问题。请问如何让查询返回第一个查询的子集而非整个jobs表的数据?


编辑:问题解决

根据建议,我将所有OR条件用括号包裹,结果恢复正常(返回11条符合要求的数据):

select `jobs`.* from `jobs`
inner join `clients` on `jobs`.`client_id` = `clients`.`id`
inner join `job_comments` on `jobs`.`client_id` = `job_comments`.`id`
where exists (
    select * from `job_users`
    where `jobs`.`number` = `job_users`.`job_number`
    and `user_id` = 77
) and 
`is_active` = 1 and
`is_complete` = 0 and 
(
    `clients`.`title` like '%se%' or
    `jobs`.`title` like '%se%' or 
    `jobs`.`number` like '%se%' or
    `job_comments`.`comment` like '%se%'
)
order by `jobs`.`number` desc;

感谢各位的指点!


内容的提问来源于stack exchange,提问作者ThurstonLevi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 12:20:32