简体   繁体   English

mysql join:获取与通知类型为零的行相匹配的所有行

[英]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_typemir_notifications与具有所有行沿表notifcation_type0

(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM