To implement multi-condition query, if the value is 0, it is not limited to any conditions. how can I use SQL? for example, if a certain type of store in a region is not limited to a Chinese query, the where Region value is not required, but only the where type of Chinese food, select the region as Beijing type for Chinese food query on... to implement multi-condition query, if the value is 0, how can I use SQL to implement unlimited conditions?
For example, to query a certain type of shops in a certain region, if the region is not limited to Chinese food, you do not need where region = value, but only where Type = Chinese food, if you select "Beijing" as "Chinese food", the query result is "where" = "Beijing AND" = "Chinese food.
There are also many other conditions. it is too complicated to query different SQL statements after using if else at the backend. I don't know much about SQL, but I think there should be corresponding solutions for SQL.
Reply content:
To implement multi-condition query, if the value is 0, how can I use SQL to implement unlimited conditions?
For example, to query a certain type of shops in a certain region, if the region is not limited to Chinese food, you do not need where region = value, but only where Type = Chinese food, if you select "Beijing" as "Chinese food", the query result is "where" = "Beijing AND" = "Chinese food.
There are also many other conditions. it is too complicated to query different SQL statements after using if else at the backend. I don't know much about SQL, but I think there should be corresponding solutions for SQL.
You can determine if and then splice the corresponding where
Ex: $ SQL = select * from xxx where 1 = 1;
If (type! = 0) {$ SQL. = 'and where Type = Chinese food ';}
Use the query condition as a Map Params; then, use a method to concatenate additional query statements;
public String getWhere(Map
params) { String str = ""; if(params.size() > 0) { boolean and = false; for(String param : params.keySet()) { Object value = params.get(param); if(value == null || value.equals(0)) { continue; } else { if(and) { str = str + " and " + param + " = " + " " + value; } else { and = true; str = str + param + " = " + " " + value; } } } } if(str != "") { str = " where " + str; } return str;}
Mybatis spell SQL
JPA fight creteria
Link condition and or
Mysql Query uses strings to pass Mysql Query commands. Therefore, it is feasible to modify the query conditions after where. you can create a string array to store these commands first, when you modify the conditions, for example, if you select an address, you can modify the corresponding values of the array, or click query to check the components on the interface, such as the meal class or address, they get their values, add them to the array, and then traverse them at the beginning of the query. if they are empty, they will not be processed. if they are not empty, they will be added to the unified condition string, finally, add it to the query language command string.
String cmd = "select * from table name where unified condition"
It may not take weeks for a newbie to consider, but the practice should be fine.