选择查询以同时检查或同时检查或检查条件 [英] Select Query to check both or either or condition

查看:104
本文介绍了选择查询以同时检查或同时检查或检查条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PostgreSQL 9.1,希望从表中选择一条记录。详细信息如下:

I'm using postgresql 9.1 and wish to select a single record from table. Details are as below :

表名:muser

表中存在的字段:userid,username,firstname,姓氏,电话号码和邮件ID

fields present in table : userid,username,firstname,lastname,phonenumber and mailid

要选择的字段:用户ID,邮件ID,电话号码

fields to be selected : userid, mailid, phonenumber

请求参数:mailid和电话号码

request parameter : mailid and phonenumber

应满足的条件:


  1. 在以下情况下显示记录两者都存在

  1. display a record when both are present

仅当存在mailid时显示记录

display a record when only mailid is present

显示一条记录

预期输出:

具有用户ID,
电话号码和
mailid的单个记录(因为userid是唯一的)

single record (as userid is unique) with userid, phonenumber and mailid

两者(如果存在)。

我尝试使用以下查询:

SELECT userid, mailid, phonenumber 
FROM muser 
WHERE phonenumber = ? OR mailid = ?

对于前两个条件工作正常,但对最后一个条件无效。条件给出查询中存在的所有记录。为什么?他们的查询有变化吗?

It's working fine for first two conditions but not working for last condition..When i fire query for last condition it gives all records present in query.Why so? Is their any changes in query? or anything else

推荐答案

当我处理过滤器时,其值可以是任何值,包括 null 我将尝试添加 coalesce()

When I'm dealing with filter where the value can be any including null I'll try add coalesce()

SELECT userid, mailid, phonenumber 
FROM muser 
WHERE coalesce(phonenumber,'no data') = coalesce(?,'no data') 
OR coalesce(mailid,'no data') = coalesce(?, 'no data');

这篇关于选择查询以同时检查或同时检查或检查条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆