编写MySQL在列中搜索多个值

时间:2016-05-24 19:16:49

标签: mysql

SELECT 
    ID, 
    Division, 
    EffectiveDate, 
    PM, 
    case Status 
        when 0 then 'Dead'
        when 1 then 'Active'
        when 2 then 'Job'
        when 3 then 'Pending'
        when 4 then 'Sales Lead'
        when 5 then 'Budget'
        when 6 then 'Change Order'
    end as Status, 
    Name, 
    Address, 
    ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status = 6
ORDER BY EffectiveDate ASC
;

我需要返回状态为6 OR 2的所有值

我尝试写AND Status = 6 or 2,但这不起作用= / 这是通过连接表来完成的还是有更简单的方法?

感谢

4 个答案:

答案 0 :(得分:0)

SELECT 
    ID, 
    Division, 
    EffectiveDate, 
    PM, 
    case Status 
        when 0 then 'Dead'
        when 1 then 'Active'
        when 2 then 'Job'
        when 3 then 'Pending'
        when 4 then 'Sales Lead'
        when 5 then 'Budget'
        when 6 then 'Change Order'
    end as Status, 
    Name, 
    Address, 
    ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND ( Status = 6 OR Status = 2 )
ORDER BY EffectiveDate ASC

答案 1 :(得分:0)

你很接近,只需要OR Status = 2语句。

SELECT 
    ID, 
    Division, 
    EffectiveDate, 
    PM, 
    case Status 
        when 0 then 'Dead'
        when 1 then 'Active'
        when 2 then 'Job'
        when 3 then 'Pending'
        when 4 then 'Sales Lead'
        when 5 then 'Budget'
        when 6 then 'Change Order'
    end as Status, 
    Name, 
    Address, 
    ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND (Status = 2 OR Status = 6)
ORDER BY EffectiveDate ASC
;

还有一些info

答案 2 :(得分:0)

您想要IN或OR -

AND Status IN (6, 2)

AND Status = 6 OR Status = 2

答案 3 :(得分:0)

您可以在查询中使用IN。 Tyr:

SELECT 
ID, 
Division, 
EffectiveDate, 
PM, 
case Status 
    when 0 then 'Dead'
    when 1 then 'Active'
    when 2 then 'Job'
    when 3 then 'Pending'
    when 4 then 'Sales Lead'
    when 5 then 'Budget'
    when 6 then 'Change Order'
end as Status, 
Name, 
Address, 
ProjectType
FROM intranet.t_bidinfo
WHERE Division = 'TI'
AND EffectiveDate >= '2015-06-01'
AND Status IN (6,2)
ORDER BY EffectiveDate ASC