首页 > 解决方案 > Mysql 通过乘以 id 过滤

问题描述

我无法完成编写查询以通过乘以 id 过滤行。这是查询:

select distinct `storage_file`.*, `storage_tag`.`id` as `tid` from `storage_file` 
inner join `storage_file_tag` on `storage_file`.`id` = `storage_file_tag`.`storage_file_id` 
inner join `storage_tag` on `storage_tag`.`id` = `storage_file_tag`.`storage_tag_id`
where `storage_file`.`user_id` = 17 and `storage_file`.`deleted_at` is null and 
`storage_tag`.`id` IN(13,17);

所以结果是没有 group by 语句是:

在此处输入图像描述

所以.. 我只需要包含 tid 13 和 17 的两条记录的结果,当我将“IN(13,17)”替换为storage_tag. id= 13 和storage_tagid= 17 - 我没有得到任何记录

我怎样才能编写像a+b但不是aOR的子查询b

标签: mysqljoinfilter

解决方案


你可以更换

... AND id IN (11,22)

... AND ( id = 11 OR id = 12)

您需要括号,因为 WHERE 运算符优先级规则非常简单。

当然,

... AND id = 11 AND id = 12

从不返回任何内容,因为 id 不能同时具有两个不同的值。


推荐阅读