mysql连接3个以上表

pu82cl6c  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(300)

我希望根据感兴趣的用户位置获取事件,并保留用户不感兴趣的事件。我也尝试过,但没有得到确切的事件数据。根据这个查询,如果uninterested\u事件表中没有记录,则不从表中获取记录。

> SELECT * FROM `event` e JOIN `user_interested_location` uil ON
> e.`city` = uil.`city` AND uil.`user_id` = 'A' JOIN
> `uninterested_event` ue ON e.`event_id` != ue.`event`
> AND ue.`user_id` = 'A' WHERE e.`start_date` >= '2018-04-14' ORDER BY
> e.`start_date` ASC

--------------------
    Event table
--------------------

event_id  | event_name | city   | start_date
event_1   |   event1   | kanpur |  2018-04-15
event_2   |   event2   | Noida  |  2018-04-20
event_3   |   event3   | Delhi  |  2018-04-14
event_4   |   event4   | Kanpur |  2018-04-17

--------------------------------
 user_interested_location table
--------------------------------

user_id  |  city   | 
   A     |  kanpur |
   B     |  Noida  |
   A     |  Noida  |

-----------------------------
 uninterested_event table
-----------------------------

user_id  | event_id | 
   A     |  event_1 |
   B     |  event_2 |

 ------------------------------
      output (user_id for A)
 ------------------------------

event_id  |  user_id   | city   | start_date
event_4   |   event4   | Kanpur | 2018-04-17
event_2   |   event2   | Noida  | 2018-04-20
njthzxwz

njthzxwz1#

试试这个:

SELECT * FROM `event` e
left JOIN `user_interested_location` uil ON e.`city` = uil.`city` AND uil.`user_id` = 'A' 
left JOIN `uninterested_event` ue ON e.`event_id` = ue.`event` and ue.`user_id` = 'A'
WHERE e.`start_date` >= '2018-04-14' and ue.user_id is null
ORDER BY e.`start_date` ASC

相关问题