【发布时间】:2023-03-26 20:20:02
【问题描述】:
我有一个表,它将用户和客人的订阅保存到 topic_names。
这是示例,现在我需要支持一堆复杂的查询,例如,get all the users who are subscribed to 'so' and 'all' or are subscribed to 'so2' 简而言之:so && (all || so2) 我首先尝试通过having clause 进行操作,但看起来同一列不起作用这种情况,所以我想出了这个:
select *
from `wp_push_notification_topics`
where exists(select *
from `wp_push_notification_topics` as `wp_laravel_reserved_0`
where `wp_push_notification_topics`.`user_id` = `wp_laravel_reserved_0`.`user_id`
and `topic_name` = 'so'
and exists(select *
from `wp_push_notification_topics`
where `wp_laravel_reserved_0`.`user_id` = `wp_push_notification_topics`.`user_id`
and `topic_name` = 'all'
or exists(select *
from `wp_push_notification_topics` as `wp_laravel_reserved_1`
where `wp_push_notification_topics`.`user_id` = `wp_laravel_reserved_1`.`user_id`
and `topic_name` = 'so2')))
效果很好。
但即使我改变:
and `topic_name` = 'all'
到
and `topic_name` = 'all22'
我得到这个结果:
这显然与之前的结果完全相同,因此是错误的!不能包含 user_id 2 行,这意味着我做错了,请帮忙。
【问题讨论】:
标签: mysql group-by sum boolean-logic having