如何优先于或超过mysql的其他条件?

时间:2017-09-12 07:10:23

标签: mysql

我有一个使用子查询的SQL查询。

SELECT t.id FROM subjects t 
where t.topic_id IN (13,11) 
  AND t.id  not in (select subject_id 
                    from user_visited_subjects uvs
                    where uvs.user_id = 3 )  
  OR t.id in (select subject_id
              from user_subject_suggestions uss 
              where uss.user_id = 3 )  
limit 10

我想给予优先级或条件意味着sql应检查查询中是否有任何记录

OR t.id in (select subject_id from user_subject_suggestions uss where uss.user_id = 3 )

然后检查其他条件。

请告知。

由于

1 个答案:

答案 0 :(得分:0)

只需使用括号。

示例:

SELECT t.id FROM subjects t 
WHERE t.id IN (SELECT subject_id FROM user_subject_suggestions uss WHERE uss.user_id = 3 )
    OR (t.topic_id IN (13,11) 
        AND t.id  NOT IN (SELECT subject_id FROM user_visited_subjects uvs WHERE uvs.user_id = 3 ))
LIMIT 10