2012-07-23 67 views
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  | 
+----------+------------+ 

我正在使用下面的查詢。這表明,如果插孔以下表2中的用戶或插孔中的用戶名在表1mysql內部連接需要另一種連接類型

SELECT t1.* 
FROM TABLE1 t1 
INNER JOIN table2 t2 
ON t1.username = t2.following 
AND (t2.username = 'jack' OR t1.username = 'jack'); 

現在我想做到這一點,顯示如果傑克LIKESFRUIT表3中從TABLE1記錄FROM表1中的所有記錄。

+0

你是什麼意思「*顯示從TABLE1記錄,如果平均... *「?你的意思是如果條件滿足,則從'TABLE1'返回所有記錄?或者是其他東西? – eggyal 2012-07-23 11:26:43

+0

提供預期結果 – Madhivanan 2012-07-23 11:28:25

+0

我的解決方案是否適合您? – 2012-07-23 13:00:27

回答

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

雖然我不知道你是什麼意思的 「FROM表1顯示的記錄」

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