Multi-condition query _ MySQL

Source: Internet
Author: User
I have posted a post on the forum about multi-condition query. Some netizens were interested in this issue a few days ago. I spent some time sorting it out. In fact, there is nothing special about it, the key lies in writing correct SQL query statements. because each user uses different databases, the SQL statements are the same. Therefore, here I only provide SQL statements that meet the requirements automatically generated based on multiple conditions. I used to post on the forum about multi-condition query. Some netizens were interested in this issue a few days ago. I spent some time sorting it out. In fact, there is nothing special about it, the key lies in writing correct SQL query statements. because each user uses different databases, the SQL statements are the same. Therefore, here I only provide SQL statements that meet the requirements automatically generated based on multiple conditions. There is basically no limit on the number of conditions, and the number of group elements cannot exceed the number of arrays.
To put it bluntly, it is actually a string game. Join in. The array storage conditions and query statements are used here. The program is not long and detailed annotations won't help you much effort. Haha.
Enter your query conditions in conditions_search.html and click query. After the input conditions are submitted to display_search_ SQL .php for processing, an appropriate SQL statement is provided.


/************** Conditions_search.html ****************/


Conditions_Search multi-condition query


Conditions_Search multi-condition query:



/************** Conditions_search.html **********************/


/************** Display_search_ SQL .php *********************/
// Condition1, condition2, condition3, and condition4.
// There is a text box input string, there is a drop-down list box selected,
// These receiving conditions are not important.
// The condition can also be added (no limit, of course, not too large, exceeds the upper limit of the array ).
// Note the following:
// When the condition value is null, no restriction is imposed;
// The name of the table to be queried is testtable;
// The field name corresponding to condition1 is column1 ......
//

$ ConditionsNumber = 4; // There are 4 conditions in total. (The actual number of conditions can be changed)
$ ConditionsArray = array ("$ condition1", "$ condition2", "$ condition3", "$ condition4 ");
// Enter each condition into an array to facilitate the following loop. (Arrays can be easily expanded)
$ SearchSQLArray = array ("where column1 = '$ condition1'", "where column2 like '% $ condition2 %'", "where column3 = '$ condition3 '", "where column4 = '$ condition4 '");
// Pre-write some SQL statements and process them as needed. (Arrays can be easily expanded)

For ($ I = 0; $ I <$ ConditionsNumber; $ I ++)
{
If ($ ConditionsArray [$ I] = "")
$ SearchSQLArray [$ I] = "";
// Step 1: If the condition value is null, the corresponding SQL statement is empty.
$ HaveWhere = false; // set the initial value of the "exist where" check flag to false.
For ($ j = 0; $ j <$ I; $ j ++)
// From the start to the current loop I, which of the following are the processing items?
// Change to and.
{
$ WherePosition = strpos ($ SearchSQLArray [$ j], "where ");
// Check whether there is a where before I.
If ($ wherePosition = "1") & ($ haveWhere = false ))
{
$ SearchSQLArray [$ I] = ereg_replace ("where", "and", $ SearchSQLArray [$ I]);
// The position of where is 1, and there is a where in front.
// Replace where with and.
$ HaveWhere = true; // "exist where" check flag is set to true.
}
}
};

For ($ I = 0; $ I <$ ConditionsNumber; $ I ++)
$ SQL = $ SQL. $ SearchSQLArray [$ I];
$ SQL = "select * from mytable". $ SQL .";";
// Form an SQL statement
Echo $ SQL;
?>
/************* Display_search_ SQL .php *********************/


Excerpt: Chinese PHP users

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.