0

我需要从 't'(线程)返回所有包含 'menu_item_id' = 1 的项目。

另外,我需要根据f.tag对以上所有结果进行过滤。

SELECT t.*, u.username AS owner, uu.username AS replyname, a.location
FROM thread t
INNER JOIN filter_thread as ft ON t.thread_id = ft.thread_id
INNER JOIN filter as f ON f.filter_id = ft.filter_id
INNER JOIN users u ON t.owner_id = u.id 
INNER JOIN users uu ON t.last_post_id = uu.id
INNER JOIN user_profiles AS up ON u.id = up.user_id
INNER JOIN avatars AS a ON up.avatar_id = a.id
WHERE t.menu_item_id='1'
AND f.tag LIKE '%hello%'
OR f.tag LIKE '%world%'

问题区域:AND f.tag LIKE '%hello%' OR f.tag LIKE '%world%'

要意识到的重要一点是,'hello' 和 'world' 可能并不总是在查询中。此外,可能还会添加 3 个搜索,但menu_item_id始终会搜索。

我不希望所有结果都需要每个 f.tags,我只想让结果包含它们中的任何一个(OR)。

任何帮助,将不胜感激。

4

5 回答 5

3

比如……括号?

SELECT t.*, u.username AS owner, uu.username AS replyname, a.location
FROM thread t
INNER JOIN filter_thread as ft ON t.thread_id = ft.thread_id
INNER JOIN filter as f ON f.filter_id = ft.filter_id
INNER JOIN users u ON t.owner_id = u.id 
INNER JOIN users uu ON t.last_post_id = uu.id
INNER JOIN user_profiles AS up ON u.id = up.user_id
INNER JOIN avatars AS a ON up.avatar_id = a.id
WHERE t.menu_item_id='1'
AND (
    f.tag LIKE '%hello%'
 OR f.tag LIKE '%world%'
)

如果问题是这个查询是动态进行的,只需1在前面添加一个。

于 2012-07-26T15:45:39.450 回答
2

使用括号指定ANDand的优先级OR

a AND (b OR c)

对比

(a AND b) OR c

例如:

WHERE t.menu_item_id='1'
  AND (    f.tag LIKE '%hello%'
        OR f.tag LIKE '%world%'
      )

这将仅返回具有 menu_item_id = '1' 并且具有包含 'hello' 或 'world' 的标签的行。

于 2012-07-26T15:45:25.553 回答
0

下面是通过 PHP 动态生成的最终代码:

    $sql_select = "SELECT t.*, u.username AS owner, uu.username AS replyname, a.location";
    $sql_from = " FROM ";
    $sql_where = " WHERE ";
    $sql_joins = "";
    $sql_order = " ORDER BY t.replystamp DESC
                    LIMIT $threadstart, $limit";  
    $sql_from .= ' thread t ';
    $sql_where .= " t.menu_item_id=$menu_item_id";
    $sql_joins .= ' INNER JOIN filter_thread as ft ON t.thread_id = ft.thread_id
                    INNER JOIN filter as f ON f.filter_id = ft.filter_id
                    INNER JOIN users u ON t.owner_id = u.id 
                    INNER JOIN users uu ON t.last_post_id = uu.id
                    INNER JOIN user_profiles AS up ON u.id = up.user_id
                    INNER JOIN avatars AS a ON up.avatar_id = a.id';
    if ($ac_total > 0) {
        $sql_where .= " AND (";
        for ($i=0; $i < $ac_total; $i++)
        {
            if ($i == 0)
            {
                $sql_where .= "f.tag LIKE '%$active_category[$i]%'";
            } else {
                " OR f.tag LIKE '%$active_category[$i]%')";
            }
        }
        $sql_where .= ")";
    }
    $sql = $sql_select . $sql_from . $sql_joins . $sql_where . $sql_order;
    $query = $this->db->query($sql);

谢谢您的帮助!

于 2012-07-26T16:30:00.103 回答
0

我不确定这是否能回答你的问题,但我喜欢用括号让事情更容易阅读,就像这样

SELECT t.*, u.username AS owner, uu.username AS replyname, a.location
FROM thread t
INNER JOIN filter_thread as ft ON t.thread_id = ft.thread_id
INNER JOIN filter as f ON f.filter_id = ft.filter_id
INNER JOIN users u ON t.owner_id = u.id 
INNER JOIN users uu ON t.last_post_id = uu.id
INNER JOIN user_profiles AS up ON u.id = up.user_id
INNER JOIN avatars AS a ON up.avatar_id = a.id
WHERE t.menu_item_id='1'
AND (f.tag LIKE '%hello%'
OR f.tag LIKE '%world%')

现在它说 where t.menu_item_id = 1 AND 括号中的任一条件为真,而不是说 (t.menu_item_id = 1 AND f.tag LIKE '%hello%') OR f.tag LIKE '%world% '

希望有帮助。

于 2012-07-26T15:46:51.733 回答
0

为什么不用括号?

WHERE t.menu_item_id='1' AND (f.tag LIKE '%hello%' OR f.tag LIKE '%world%') 
于 2012-07-26T15:47:52.183 回答