我仍然试图掌握SQL。我构建了一个查询,它从filter_thread(包含'filter_id'和'thread_id'列)表和过滤器('filter_id和'tag')表中提取thread_id'
然后,我使用完全不同的查询来查找包含'thread_id'的线程表内容。
我意识到这不是最好的方法,但无法获得成功的查询。有人能帮忙吗?
$query = "SELECT ft0.thread_id
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
$result = $query->result_array();
$thread = array();
foreach ($result as $thread_id)
{
$id = $thread_id['thread_id'];
$query = $this->db->query("SELECT * FROM thread WHERE thread_id='$id'");
$thisRow = $query->result_array();
array_push($thread, $thisRow[0] );
}
谢谢!
答案 0 :(得分:6)
您可以在单个查询中执行此操作,如下所示:
SELECT t.*
FROM filter AS f0
INNER JOIN filter_thread AS ft0
ON ft0.filter_id = f0.filter_id
INNER JOIN thread AS t
ON ft0.thread_id = t.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%
答案 1 :(得分:1)
select t.x, t.y, ... from thread t
inner join filter f on f.thread_id = t.thread_id
inner join filter_thread ft on ft.filter_id = f.filter_id
where bla bla
可能有一些小组......?
答案 2 :(得分:1)
试试这个
SELECT t.*, f0.*
FROM filter f0
INNER JOIN filter_thread ft0 ON ft0.filter_id = f0.filter_id
INNER JOIN thread t ON t.thread_id = ft0.thread_id
WHERE f0.tag LIKE '%filter1%'
OR f0.tag LIKE '%filter2%'"
GROUP BY f0.filter_id