除了多个OR之外,还需要WHERE AND子句

时间:2012-07-26 15:43:21

标签: mysql

我需要从'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)。

任何帮助都将不胜感激。

5 个答案:

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

答案 1 :(得分:2)

使用括号指定ANDOR的优先顺序。

a AND (b OR c)

VS

(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'AND的行,其中包含'hello'或'world'标记。

答案 2 :(得分: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%')

现在它说t.menu_item_id = 1并且括号中的任何一个条件都是真的,而不是说(t.menu_item_id = 1 AND f.tag LIKE'%hello%')或者f.tag LIKE' %世界%'

希望有所帮助。

答案 3 :(得分:0)

为什么不使用括号?

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

答案 4 :(得分: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);

感谢您的帮助!