简体   繁体   中英

query sql table By First_Name and/or Last_Name

I need to write a query that query customer table base on first_name and/ or last_name. in my query it works for example when I run with firstname =Ann it brings all the customers with that firstname and when I run with lastname=sam brings all will that last name, but when I run with 'Ann','Sam' instead of bring only one record that is match this it brings all with firstname Ann or last same sam so it bring several records.

 select * from customer
where WHERE  --(CONVERT(varchar(50),decryptbykey([Account_Number]))=                     
  @UserName or ce.Email= @UserName or Username=@UserName )
  ((CONVERT(varchar(50),decryptbykey([First_Name]))) =@First_Name) 
  and (CONVERT(varchar(50),decryptbykey([Last_Name])) =@Last_Name) 
  or  ((CONVERT(varchar(50),decryptbykey([First_Name]))) =@First_Name) 
 or (CONVERT(varchar(50),decryptbykey([Last_Name])) =@Last_Name) 

In this scenario I'd treat each parameter as optional. If provided then enforce the condition, otherwise skip that check. This type of query is convenient in the sense that you do not have to maintain n! versions of your query, however I must warn you this is prone to bad cached query plans.

If you experience a significant slowdown that you cannot explain you might want to tack 'OPTION (RECOMPILE)' to the end of you query. In general this may seem like a bad idea but I'd rather SQL server take an extra 5 ms each query than take 30 seconds once. Locks, database load, user experience, etc. As usual, forcing query options is generally a bad idea and should only be done if there is no other reasonable solution.

IF (@UserName_Name = '')
    SET @First_Name = NULL;
IF (@First_Name = '')
    SET @First_Name = NULL;
IF (@Last_Name = '')
    SET @First_Name = NULL;

select *
from customer
where (@UserName IS NULL
        OR Username = @UserName
        OR Email = @UserName
        OR CONVERT(varchar(50), decryptbykey([Account_Number])) = @UserName)
    AND (@First_Name IS NULL OR CONVERT(varchar(50), decryptbykey([First_Name])) = @First_Name)
    AND (@Last_Name IS NULL OR CONVERT(varchar(50), decryptbykey([First_Name])) = @Last_Name);

This might not be pretty, but it works as you describe it.

If exact match on both Firstname AND Lastname only the exact match is returned (could be more than one though). If match on only Firstname OR Lastname, all matching records are returned

SELECT * FROM customer 
WHERE 
(
  (First_name = (CASE WHEN (First_name = @First_Name AND Last_name = @Last_Name) THEN @First_Name END)) 
  AND
  (Last_name = (CASE WHEN (First_name = @First_Name AND Last_name = @Last_Name) THEN @Last_Name END))
)
OR 
(
  (First_name = (CASE WHEN (First_name = @First_Name AND Last_name <> @Last_Name) THEN @First_Name END)) 
  OR 
  (Last_name = (CASE WHEN (First_name <> @First_Name AND Last_name = @Last_Name) THEN @Last_Name END))
)

You'll have to add your other filters and CONVERTs yourself.

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