简体   繁体   中英

SQL Select from 4 tables on 2 columns

I have 4 tables. I will write only important columns.

Users (u)    - id
Friends (f)  - sender_id, receiver_id
Fans (fan)   - sender_id (it is me), receiver_id
Activity (a) - sender_id, receiver_id

I need to get activities from my friends and from people whom I support.

It means that there are these possibilities:

1) u.id = f.sender_id   AND f.receiver_id   = a.sender_id
2) u.id = f.sender_id   AND f.receiver_id   = a.receiver_id
3) u.id = f.receiver_id AND f.sender_id     = a.sender_id
4) u.id = f.receiver_id AND f.sender_id     = a.receiver_id
5) u.id = fan.sender_id AND fan.receiver_id = a.sender_id
6) u.id = fan.sender_id AND fan.receiver_id = a.receiver_id

Is there any other solution than using UNION? The database structure cannot be changed it is from bought extension.

Thank you!

Yes, I believe you can use case statement. To make sure, please post your query using UNION then I'll convert it for you to case statement.

Or you can use the link below as a reference: Case statement in MySQL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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