Urgent solution: MYSQL multi-condition query-php Tutorial

Source: Internet
Author: User
Urgent solution: MYSQL multi-condition query problem
Three conditions are available. the expected functions are as follows: input condition 1: query results that meet condition 1; input condition 2: query results that meet condition 2; input condition 3, query the results that meet condition 3. input conditions 1 and 2. query the results that meet condition 1 and 2. input conditions 1 and 3. query the results that meet condition 1 and 3; input conditions 2, 3: query results that meet conditions 2 and 3; input conditions 1, 2, and 3: query results that meet conditions 1 and 2. Result 3: Code is pasted out. There are seven tables in total. I don't know how to write the following conditions? Can this code be optimized? Solve it !!

$ Result = mysql_query ("SELECT m. tableMode, m. tableModeNote, c. tablechannel, o. tableOperator
FROM mode_table as m, mode_operator_relation as mo, operator_table as o, mode_provider_relation as mp,
Provider_table as p, channel_provider_relation as cp, channel_table as c
WHERE mo. tableModeId = m. tableModeId
AND o. tableOpeId = mo. tableOpeId
AND mp. tableModeId = m. tableModeId
AND mp. tableProId = p. tableProId
AND p. tableProId = cp. tableProId
AND cp. tableChaId = c. tableChaId
AND condition 1 condition 2 condition 3


Reply to discussion (solution)

If (condition 1 ){
$ SQL. = "and xxx1 ";
}
If (condition 2 ){
$ SQL. = "and xxx2 ";
}
If (condition 3 ){
$ SQL. = "and xxx3 ";
}
SQL statement optimization follows explain

If (condition 1 ){
$ SQL. = "and xxx1 ";
}
If (condition 2 ){
$ SQL. = "and xxx2 ";
}
If (condition 3 ){
$ SQL. = "and xxx3 ";
}
SQL statement optimization follows explain



Thank you. I have written this. Just sent a help post, you can help me answer the http://bbs.csdn.net/topics/391885406

You can use different conditions to call different statements.

You can use different conditions to call different statements.


Thank you. I have written this article. it's a great method!

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.