2012-05-08 76 views
-1

這是我當前的查詢:mySQL內部連接查詢 - 我如何結合這兩個查詢?

SELECT DISTINCT t.* FROM filter AS f 
INNER JOIN filter_thread AS ft 
ON ft.filter_id = f.filter_id 
INNER JOIN thread AS t 
ON ft.thread_id = t.thread_id 
WHERE f.tag LIKE '%test%' 
ORDER BY t.replystamp DESC 

它返回一組數據,包括「owner_id」一欄。

的「USER_TABLE」包含匹配的標識,標籤,「ID」

我以前用過這個查詢獲取數據:

SELECT username FROM users WHERE id='$user_id'" 

我需要這些在同一個查詢中出現!但是添加額外的INNER JOIN不起作用。

回答

2

也許這:

SELECT DISTINCT t.*,u.username 
FROM filter AS f 
INNER JOIN filter_thread AS ft 
ON ft.filter_id = f.filter_id 
INNER JOIN thread AS t 
ON ft.thread_id = t.thread_id 
INNER JOIN users AS u 
ON u.id = t.owner_id 
WHERE f.tag LIKE '%test%' 
ORDER BY t.replystamp DESC 

只是一個猜測。

1

嘗試以下操作:

SELECT DISTINCT t.*, u.username FROM filter AS f 
    INNER JOIN filter_thread AS ft 
     ON ft.filter_id = f.filter_id 
    INNER JOIN thread AS t 
     ON ft.thread_id = t.thread_id 
    INNER JOIN users AS u 
     ON ft.owner_id = u.id 
    WHERE f.tag LIKE '%test%' 
    ORDER BY t.replystamp DESC 
+0

都工作像一個魅力。只能接受一個答案,所以我會選擇第一個答案。 –