0
[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 中。

4

3 回答 3

0
 SELECT t1.* 
 FROM TABLE1 t1 
 INNER JOIN table2 t2 
 ON t1.username = t2.following 
left join tablet3 t3 on t3.username = t1.username
 AND (t2.username = 'jack' OR t1.username = 'jack')
AND (t3.username = 'jack' and t3.LIKESFRUIT <> '' and t3.LIKESFRUIT is not null)
;

虽然我不确定“显示 TABLE1 中的记录”是什么意思

于 2012-07-23T11:28:25.350 回答
0

尝试这个

SELECT t1.* 
 FROM TABLE1 t1 
 INNER JOIN table2 t2 
 ON t1.username = t2.following 
 AND (t2.username = 'jack' OR t1.username = 'jack')
 and exists(select * from TABLE3 t3 where t3.username=t1.username)
于 2012-07-23T11:31:09.433 回答
0
SELECT
    username, post, fruit
FROM
    TABLE1 t1
INNER JOIN
    TABLE2 t2
ON
    t1.username = t2.username
AND t1.username = t2.follwing
INNER JOIN
    TABLE3 t3
ON
    t3.LIKESFRUIT = t1.fruit
WHERE
    t1.username = 'jack';
于 2012-07-23T14:39:17.373 回答