正在覆盖我最近的帖子
fetch results using join Mysql

我做了一些尝试。我要在扫管w上

TABLE users
USERID     NAMES     EMAIL
1          KAT       [email protected]
2          JOHN      [email protected]
3          FIK       [email protected]
4          PET       [email protected]
5          COW       [email protected]


TABLE comunity
COMUNITY_ID  USERID                       FANS
1            1 KAT  #FROM TABLE USERS     [email protected] # FIK IS MY FAN
2            2 JOHN #FROM TABLE USERS     [email protected] # AM FAN OF JOHN
3            1 KAT  #FROM TABLE USERS     [email protected] # COW IS MY FAN
4            4 PET  #FROM TABLE USERS     [email protected] # AM FAN OF PET
5            1 KAT  #FROM TABLE USERS     [email protected] # JOHN IS MY FAN


我想让所有在粉丝栏中有我的电子邮件的粉丝
我让他们这样-这有效

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


现在问题在这里

我也想获得那些让他们通过电子邮件发送给粉丝专栏的用户
我是这样做的
选择我的用户ID ='1'的粉丝

所以我需要在这里更正

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");

最佳答案

您可以尝试以下查询:

SELECT * from
(
SELECT * from community where USERID=1 OR FANS='[email protected]'
) c
left join
users u
ON u.USERID = c.USERID;

08-16 23:47