[TABLE1]
+----------+-------------------+-------------+
| USERNAME | POST | FRUIT |
+----------+-------------------+-------------+
| Bob | 'Hi There' | APPLE |
| Jack | 'Hello' | ORANGE |
| Bob | 'Today is Monday' | APPLE |
| Tony | 'That is ok' | PEAR |
+----------+-------------------+-------------+
[TABLE2]
+----------+-----------+
| USERNAME | FOLLOWING |
+----------+-----------+
| Mike | Jack |
| Jack | Bob |
| Bob | Jack |
| Jack | Mike |
+----------+-----------+
[TABLE3]
+----------+----------- +
| USERNAME | LIKESFRUIT |
+----------+----------- +
| Mike | APPLE |
| Jack | ORANGE |
| Bob | BERRY |
| Jack | PEAR |
+----------+------------+
我正在使用以下查询。如果 jack 跟随 TABLE2 中的用户或 jack 在 TABLE 1 中的用户名中,它将显示 TABLE1 中的所有记录。
SELECT t1.*
FROM TABLE1 t1
INNER JOIN table2 t2
ON t1.username = t2.following
AND (t2.username = 'jack' OR t1.username = 'jack');
现在我想这样做并显示 TABLE1 中的记录,如果 Jack LIKESFRUIT 在 TABLE3 中。