简体   繁体   中英

search criteria for mysql

Suppose I have a URL which consist of 3 parameters(A,B,C) where the client can send me any of the parameters as null or all can be null.

Now I need to find a way to retrieve records if the parameters are not null.

Suppose A is not null retrieve based on A.If B is not null retrieve based on B. If C is not null retrieve based on C.

Or

If A and B is not null retrieve based on A and B

(something like where A='something' AND B='something' ).

If A and C is not null retrieve based on A and C. If C and B is not null retrieve based on C and B. or

If everything is not null retireve based on that ( means like where A='something' AND B='something' AND C='something')

Now one way I can do is create a lot of switch case other ways is using mysql query like this

SELECT a.*,l.* FROM TabelA a INNER JOIN TableL l ON a.A= l.A WHERE
(
 a.A= IFNULL(NULL,a.A) AND
 a.B= IFNULL(NULL,a.B) AND
 a.C= IFNULL(NULL,a.C)
)

Is there any other ways apart from these two ways

String query="SELECT a.*,l.* FROM TabelA a INNER JOIN TableL l ON a.A= l.A WHERE 1=1";
if (param1){
   query+=" AND Something="+param1;
}
...

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