查询使用同一列中的多个LIKE?

时间:2015-01-20 12:21:47

标签: sql mysqli

我的数据库正在列出工作。我希望用户能够查询来自特定行业的作业,但是可以根据需要从type列中选择尽可能多的工作类型,例如:

SELECT * FROM `rec_jobs` 
WHERE `industry` LIKE '%Security%' AND 
   (`type` LIKE '%Full-Time%' OR 'type' LIKE '%Part-Time%' OR 'type' LIKE '%Casual%' OR 'type' LIKE '%Contract%');

这应该返回类似:

  

ID - Industy - 输入

     
     

1 - 安全 - 兼职

     

2 - 安全 - 全职

     

3 - 安全 - 休闲

     

4 - 安全 - 全职

但它没有按预期工作 - 我没有得到任何SQL错误或任何结果(虽然我知道行存在)。

有没有人知道实现这一目标的更好方法(或在Google中搜索的正确术语)?

4 个答案:

答案 0 :(得分:5)

您应该在列名称类型周围使用相同的引号:

SELECT *
FROM `rec_jobs`
WHERE `industry` LIKE '%Security%'
    AND (
        `type` LIKE '%Full-Time%'
        OR `type` LIKE '%Part-Time%'
        OR `type` LIKE '%Casual%'
        OR `type` LIKE '%Contract%');

答案 1 :(得分:2)

如果您知道要查找的确切字符串,请使用IN()

SELECT * FROM `rec_jobs` 
WHERE `industry` = 'Security' 
AND `type` IN ('Full-Time', 'Part-Time', 'Casual', 'Contract')

答案 2 :(得分:2)

您使用了

'type' 

虽然它应该是

`type`

答案 3 :(得分:1)

SELECT * FROM rec_jobs 
WHERE type IN ('Full-Time', 'Part-Time', 'Casual', 'Contract')
AND industry`='Security'
相关问题