如何使用两个LIKE条件?

时间:2015-07-02 00:30:31

标签: oracle sql-like

我需要找到名字中有A和M的学生,但是他们的姓氏中没有a和m。这是我的查询,但由于某种原因,它一直显示有M的姓氏......我做错了什么?

SELECT (firstname || ' ' || lastname) AS "FullName"
  FROM A5
 WHERE Lower(FirstName) LIKE '%a%m%' 
   AND Lower(LastName) NOT LIKE '%a%m%'
ORDER BY LastName,
         FirstName

2 个答案:

答案 0 :(得分:1)

WHERE子句应该是:

WHERE (Lower(FirstName) LIKE '%a%m%'
OR Lower(FirstName) LIKE '%m%a%')
AND Lower(LastName) NOT LIKE '%a%'
AND Lower(LastName) NOT LIKE '%m%'

您只包含FirstName有一个后跟一个m的情况。类似地,您只排除了LastName同时具有AND和m的情况,其中a位于m之前。

答案 1 :(得分:1)

SELECT (firstname || ' ' || lastname) AS "FullName"
FROM A5
WHERE (lower(firstname) like '%a%' and lower(firstname) like '%m%') and ((lower(lastname) like '%a%' and lower(lastname) not like '%m%') or (lower(lastname) not like '%a%' and lower(lastname) like '%m%') )
ORDER BY LastName,FirstName

试试这个。