[英]mysql join : get all rows that matches the rows that matches notification type is zero
I have two tables in my database 我的数据库中有两个表
1.mir_notifications (id(PK),title,message,notifcaton_type) 1.mir_notifications(id(PK),标题,消息,notifcaton_type)
2.mir_users_notifications (id,user_id,notif_id(FK)) 2.mir_users_notifications(id,user_id,notif_id(FK))
notif_id
refers to id
in the mir_notification
table notif_id
引用mir_notification
表中的id
I want to fecth all rows from mir_notification
table that matches a user_id in mir_users_notifcations
table and notification_type
in mir_notifications
table along with all rows that has notifcation_type
is 0
我想fecth所有行
mir_notification
匹配的USER_ID表mir_users_notifcations
表和notification_type
在mir_notifications
与具有所有行沿表notifcation_type
是0
(ie if notifcation type is zero it will fetch for all users ) (即, 如果通知类型为零,它将为所有用户获取 )
Here is the query i am used 这是我使用的查询
SELECT mir_notifications.* FROM mir_notifications LEFT JOIN mir_users_notification ON mir_notifications.id=mir_users_notification.notif_id WHERE mir_notifications.notfication_type IN (0,2) AND mir_users_notification.user_id=2
Here i am passing notification type 0 and 1 and userid is 2 ,currently this user have no special notification but he have a common notification (ie notification_type is 0) so it should returs rows from mir_notification
table that matches notification_type
is zero but it will returns empty results 在这里,我传递的通知类型为0和1,并且userid为2,当前此用户没有特殊通知,但是他有一个公共通知(即,notification_type为0),因此它应从
mir_notification
表中匹配notification_type
为零的行,但它将返回空结果
You can simply modify your WHERE
clause. 您可以简单地修改
WHERE
子句。 The query below will get all notifications of type 2
for user 2
, along with all notifications of type 0
. 下面的查询将获取用户
2
所有类型2
通知,以及所有类型0
通知。
SELECT mir_notifications.* FROM mir_notifications LEFT JOIN mir_users_notification ON mir_notifications.id=mir_users_notification.notif_id WHERE (mir_notifications.notfication_type = 2 AND mir_users_notification.user_id=2) OR (mir_notifications.notification_type = 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.