mySQL内部联接查询 - 如何组合这两个查询?

时间:2012-05-08 18:13:17

标签: 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,标记为'id'

我之前使用此查询来获取数据:

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

我需要在同一个查询中出现这些!但添加额外的INNER JOIN无效。

2 个答案:

答案 0 :(得分: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 :(得分: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