通过特定的两个名称mysql查找记录

时间:2016-05-13 06:00:05

标签: php mysql join inner-join

我想在单列中通过两个特定名称获取记录,但此查询对我不起作用,

SELECT * 
FROM orders_custom oc 
    INNER JOIN group_options go 
    ON oc.group_option_id = go.group_option_id 
    WHERE orderedProduct_id = '40476' 
        AND go.is_default = 1 
        AND oc.group_name = 'size' 
        AND oc.group_name = 'base'

写它的正确方法是什么?

3 个答案:

答案 0 :(得分:2)

尝试此查询

SELECT * FROM orders_custom oc 
INNER JOIN group_options go ON oc.group_option_id = go.group_option_id 
WHERE orderedProduct_id = '40476' 
    AND go.is_default = 1 
    AND oc.group_name in ('size','base')

或者您可以在OR子句

中指定名称条件
AND (oc.group_name = 'size' OR oc.group_name = 'base' OR ...)

答案 1 :(得分:2)

最后oc.group_name您应该使用ORIN

答案 2 :(得分:1)

您需要使用OR,请参阅以下示例:

SELECT * FROM orders_custom oc 
INNER JOIN group_options go 
    ON oc.group_option_id = go.group_option_id 
WHERE orderedProduct_id = '40476' AND go.is_default = 1 AND (oc.group_name = 'size' OR oc.group_name = 'base')