select first_name,last_name from contacts where first_name = 'somevalue' and last_name = 'somevalue'
我通过ajax获取这些值
示例表联系人
id | first_name | last_name
1 | Rahul | Roy
2 | Rohit | NULL
现在当通过ajax我得到first_name作为" Rahul"和last_name为" Roy"查询返回1条记录(第一条)
但如果我得到first_name作为" Rohit"并且last_name为空我得到以下查询
select first_name,last_name from contacts where first_name = 'Rohit' and last_name = ''
我没有记录。但实际上我想从上表中获取id为2的记录。
我的查询如何只在一个查询中填满这两个条件
答案 0 :(得分:3)
SELECT first_name,last_name
FROM contacts
WHERE first_name = 'Rohit' and (last_name IS NULL OR last_name = 'your value')
你走了!
答案 1 :(得分:0)
对于这种情况USE NULL,你能试试吗?
select first_name,last_name
from contacts
where first_name = 'Rohit' and last_name = ''
到
select first_name,last_name
from contacts
where first_name = 'Rohit'
and (last_name IS NULL || last_name='somevalue')
如果您希望姓氏不为空
select first_name,last_name
from contacts
where first_name = 'Rohit'
and last_name IS NOT NULL
答案 2 :(得分:0)
使用“is null”而不是“=''”。
即
select first_name,last_name from contacts where first_name = 'Rohit' and last_name is null
答案 3 :(得分:0)
select first_name,last_name
from contacts
where first_name = 'Rahul' and (last_name = 'Roy' or last_name is null);