[英]Sql query with optional fields for Search Web Application
Not an SQL expert: 不是SQL专家:
I am currently implementing a Searching Web Application. 我目前正在实施搜索Web应用程序。 The user will send a
GET request
to a particular Endpoint
. 用户将向特定
Endpoint
发送GET request
。
The Endpoint
will accept a set of URL Params
and the request can come with optional fields. Endpoint
将接受一组URL Params
,请求可以带有可选字段。 Such as 1, 2 or 3 fields. 如1,2或3个字段。
My Database table for USER
looks like this. USER
我的数据库”表格如下所示。
+--------------+---------+------+-----+---------+----------------+
| id | firstName | lastName | email | zip | phone |
+--------------+---------+------+-----+---------+----------------+
Now the web application will get a GET Request
with either Phone
or Zip
or Email
. 现在,Web应用程序将得到一个
GET Request
与任何Phone
或Zip
或Email
。
I have two solutions for doing this but both look bad: 我有两个解决方案,但两个看起来都很糟糕:
Solution 1:
Have multiple SQL Queries
and Execute
them according to the URL Params, I receive. 有多个
SQL Queries
并根据URL Params Execute
它们,我收到。
Select * from User where phone=1111111111 and zip=12345 and email=test@email.com;
Select * from User where phone=1111111111 and zip=12345;
... ...
And so on........I will end up having many queries and this will be a bad implementation. 等等........我最终会遇到很多疑问,这将是一个糟糕的实现。 Also will be bad to maintain.
维护起来也不好。
Solution 2:
Other solution that I am thinking of is to have a method, which will build an SQL query based on the URL Params I receive. 我想到的其他解决方案是有一个方法,它将根据我收到的URL Params构建一个SQL查询。
Example: 例:
buildSQLQuery(phone,zip,email){
String sql = "Select * from User where "
if(phone!=null && email!=null && zip!=null ){
sql = sql + "phone = " + phone + " and zip = " + zip + " and email = " + email;
}else if (phone!=null && email!=null && zip==null){
sql = sql + "phone = " + phone + " and email = " + email;
}
.......
......
And so on have all conditions and build that particular query.
}
I don't like both these solutions. 我不喜欢这两种解决方案。
Is there a way to write a Single SQL query and that will handle all the above conditions. 有没有办法编写单个SQL查询,并将处理所有上述条件。
Something like if the URL Param value is NULL
then that should not affect the query and I will get my expected results. 如果URL Param值为
NULL
那么这应该不会影响查询,我将得到我期望的结果。
As in my case the optional values, which don't come in are set to NULL
. 在我的情况下,未进入的可选值设置为
NULL
。
Can I implement something like this? 我可以实现这样的东西吗?
Select * from User where (if notNull (phone))(phone = phone ) and (if notNull (email))(email = email ) and (if notNull (zip))(zip = zip )
If any of the above one value is null then don't use that part in where Condition
. 如果以上任何一个值为null,则不要
where Condition
使用该部分。
Also I will always have one field present, so there will be no case where all values are null. 此外,我将始终存在一个字段,因此不存在所有值都为空的情况。
I am implementing this web application in Java and Spring MVC. 我在Java和Spring MVC中实现这个Web应用程序。 If anyone can guide me in the correct direction.
如果有人能指导我正确的方向。
Thank you. 谢谢。
I think one more possible solution like: 我认为还有一个可能的解决方案:
String sql = "Select * from User where 1=1 "
if(phone!=null){
sql += " and phone = " + phone ;}
if(email!=null){
sql += " and email = " + email ;}
if(zip!=null){
sql += " and zip = " + zip ;}
OR You can try following single query: 或者您可以尝试以下单个查询:
select * from User
where (@phone is null OR phone = @phone) AND (@email is null OR email = @email) AND (@zip is null OR zip = @zip)
You can do like this: 你可以这样做:
SELECT * FROM User where(
IF($phone != 0, IF(phone = $phone,1,0), 0) AND
IF($email != 0, IF(email = $email,1,0),0) AND
IF($zip != 0, IF(zip = $zip,1,0),0)
)
assumed for PHP you can change the syntax, may be this query not do the job completely but if you provide fiddle i will modify this to give the proper result. 假设PHP可以更改语法,可能是这个查询没有完全完成工作,但如果你提供小提琴我将修改它以给出正确的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.