简体   繁体   English

如何使用不同的参数创建SQL搜索查询

[英]How to create a SQL search query with varying arguments

So I'm implementing search functionality into my application, and I need to write the SQL query that will search the database for records based on a wide range of possible parameters. 因此,我要在应用程序中实现搜索功能,并且需要编写SQL查询,该查询将根据各种可能的参数在数据库中搜索记录。 I'm including a sample of what I have so far: 我将提供到目前为止的样本:

SELECT m.*, a.*, p.*, e.*
FROM members m
  LEFT JOIN addresses a ON m.member_id = a.address_member_id AND a.preferred_address = TRUE
  LEFT JOIN phones p ON m.member_id = p.phone_member_id AND p.preferred_phone = TRUE
  LEFT JOIN emails e ON m.member_id = e.email_member_id AND e.preferred_email = TRUE
WHERE m.member_id IN (
    (SELECT address_member_id 
     FROM addresses 
     WHERE address = '123 Hart Ct' AND unit = NULL AND city = 'Hometown' AND state = 'NJ' AND zip_code = '08895'),
    (SELECT phone_member_id 
     FROM phones 
     WHERE area_code = '732' AND prefix = '619' AND line_number = '7826'),
    (SELECT email_member_id
     FROM emails
     WHERE email_address = 'craigmiller160@gmail.com')
)
AND m.first_name = 'Jane' AND m.middle_name = 'Deborah' AND m.last_name = 'Foster'
ORDER BY member_id ASC;

As you can see, I'm searching for records in the "members" table based on the values in that table, as well as the values of the "preferred" address, phone, email, etc, that are associated with it. 如您所见,我正在基于该表中的值以及与之关联的“首选”地址,电话,电子邮件等的值搜索“成员”表中的记录。

The problem is that the query above requires all of those values to be provided. 问题是上面的查询要求提供所有这些值。 In most cases, the user will only provide a handful of the actual values to perform the search. 在大多数情况下,用户只会提供一些实际值来执行搜索。

Now, I know that one option is to build the query dynamically in my application layer, concatenating Strings together and whatnot. 现在,我知道一种选择是在我的应用程序层中动态构建查询,将字符串连接在一起,而不进行其他操作。 That way the WHERE clauses would only have the values that are needed for that specific query. 这样,WHERE子句将仅具有该特定查询所需的值。 But before I do that, I'm wondering if there's any way to accomplish this in a purely-SQL way? 但是在我这样做之前,我想知道是否有任何方法可以通过纯SQL方式完成此任务?

I'm using MYSQL btw. 我正在使用MYSQL btw。 Thanks. 谢谢。

You have two options to do this in purely-SQL way 您有两种选择以纯SQL方式执行此操作

  1. The first way is using a stored procedure. 第一种方法是使用存储过程。 Using it you can use IF condition and concatenate the query string inside the stored procedure and then execute it (I can provide a demo if you want) 使用它,您可以使用IF条件并在存储过程中连接查询字符串,然后执行它(如果需要,我可以提供一个演示)

  2. The second way is using an OR condition 第二种方法是使用OR条件

     SELECT m.*, a.*, p.*, e.* FROM members m LEFT JOIN addresses a ON m.member_id = a.address_member_id AND a.preferred_address = TRUE LEFT JOIN phones p ON m.member_id = p.phone_member_id AND p.preferred_phone = TRUE LEFT JOIN emails e ON m.member_id = e.email_member_id AND e.preferred_email = TRUE WHERE m.member_id IN ((SELECT address_member_id FROM addresses WHERE ('123 Hart Ct' is NULL OR address = '123 Hart Ct') AND (NULL is NULL OR unit = NULL) AND ('Hometown' is NULL OR city = 'Hometown') AND ('NJ' is NULL OR state = 'NJ') AND ('08895' IS NULL OR zip_code = '08895')); 

Note you can replace is NULL with = "" if the variable will be empty not NULL 请注意,如果变量为空,则可以用=“”替换为NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM