简体   繁体   中英

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. 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. But before I do that, I'm wondering if there's any way to accomplish this in a purely-SQL way?

I'm using MYSQL btw. Thanks.

You have two options to do this in purely-SQL way

  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)

  2. The second way is using an OR condition

     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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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