简体   繁体   中英

mySQL : How to use a PDO bound paramter within a CASE statement of SELECT query ?

In a mySQL SELECT query I want to select rows of a client table based upon a PDO bound search parameter condition in a WHERE clause. Something like WHERE client = :searchStr

There are 2 cases : A) a search parameter contains a string or B) the search parameter is empty

In the case of A the param :searchStr contains for instance a client number '123'.

So I use : WHERE client = :searchStr and the query return client 123 if the client is in the table.

In the case of B the string is empty. So I do not use a WHERE clause and the query simply returns all clients in the table

Of course I could write an IF .. ELSE statement outside the SELECT query. But how to code it with a CASE statement inside the SELECT query ?

Something like

CASE   
   WHEN :searchStr = '' THEN WHERE client = :searchStr
END

does not work

WHERE (:searchStr = '' or client = :searchStr)

:searchStr可以为NULL而不只是使用Emprty字符串

WHERE (:searchStr is null or client = :searchStr)

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