简体   繁体   中英

Mysql join 2 table with multiple where clause

am covering my recently post fetch results using join Mysql

i made some tries. on wat i want

TABLE users
USERID     NAMES     EMAIL 
1          KAT       1@1.com
2          JOHN      2@1.com
3          FIK       3@1.com
4          PET       4@1.com
5          COW       5@1.com


TABLE comunity
COMUNITY_ID  USERID                       FANS
1            1 KAT  #FROM TABLE USERS     3@1.com # FIK IS MY FAN
2            2 JOHN #FROM TABLE USERS     1@1.com # AM FAN OF JOHN
3            1 KAT  #FROM TABLE USERS     5@1.com # COW IS MY FAN
4            4 PET  #FROM TABLE USERS     1@1.com # AM FAN OF PET
5            1 KAT  #FROM TABLE USERS     2@1.com # JOHN IS MY FAN 

i want to get all fans which have my email in fans column i get them like this - this works

SELECT * FROM  
users u 
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1' 

now the problem is here

i want to also get users which i have they emails to fans column i did it like this select fans where my userid = '1'

so i need some correction here

SELECT * FROM  
users u 
INNER JOIN comunity c ON u.USERID = c.USERID
WHERE c.FANS = 'MY USERID - 1' 
AND * IN(SELECT c.FANS FROM comunity WHERE c.USERID = 'MY USERID - 1");

You can try this Query:

SELECT * from
(
SELECT * from community where USERID=1 OR FANS='1@1.com'
) c
left join
users u
ON u.USERID = c.USERID;

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