SQL multi-condition query

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.