针对多个值检查子查询

时间:2014-09-02 08:58:39

标签: mysql sql subquery

我想针对多个值检查子查询。 类似的东西:

... WHERE (value1 OR value2) IN ([subquery])

以上行不起作用? 这甚至可能吗? 怎么做?

我现在使用了一种解决方法,但它效率不高。

SELECT id FROM project 
        WHERE 
        (   subcategory IN 
            (
                SELECT id
                FROM other_table WHERE value LIKE ?
            )
            OR subcategory2 IN 
            (               
                SELECT id
                FROM other_table WHERE value LIKE ?
            )
        )

1 个答案:

答案 0 :(得分:1)

一种方法是转换为exists子查询:

where  exists
       (
       select  *
       from    other_table
       where   value like ?
               and id in (subcategory, subcategory2)
       )