SQL statement optimization-php Tutorial

Source: Internet
Author: User
SQL statement optimization searches for keywords in two tables, and queries the id corresponding to the keyword

$ Keyword = "XX"; // keyword $ sql1 = "select id from table1 where title like" % $ keyword % "; $ result1 = $ this-> db-> getAll ($ sql1); // id array foreach ($ result1a as $ k => $ v) returned after SQL execution) {$ result1 [$ k] ['flat'] = 1; // give an ID, let me know this is found from table1} $ sql2 = "select id from table2 where title like" % $ keyword % "; $ result2 = $ this-> db-> getAll ($ sql2); foreach ($ result2a as $ k => $ v) {$ result2 [$ k] ['flat'] = 2;} $ result = array_merge_recursive ($ result1, $ result2 );


Reply to discussion (solution)

What problems do you encounter ??

1. your $ result1a and $ result1b are empty. it makes no sense to loop. it is estimated that you have entered an error.
2. the loop body seems useless.

Please describe the problem clearly

What problems do you encounter ??



1. your $ result1a and $ result1b are empty. it makes no sense to loop. it is estimated that you have entered an error.
2. the loop body seems useless.

Please describe the problem clearly



Result1a-> result1,
Result2a-> result2,

Combine sql1 and sql2 into an SQL statement to give the Mark separately.
It is equivalent to executing the SQL statement and returning the following types
Array (
Array ("id" => 4, "flat" => 1 ),
Array ("id" => 7, "flat" => 1 ),
Array ("id" => 4, "flat" => 2)
);

$ Keyword = "XX"; // keyword $ sql1 = "select id from table1 where title like" % $ keyword % "; $ result1 = $ this-> db-> getAll ($ sql1); // id array foreach ($ result1 as $ k => $ v) returned after SQL execution) {$ result1 [$ k] ['flat'] = 1; // give an ID, let me know this is found from table1} $ sql2 = "select id from table2 where title like" % $ keyword % "; $ result2 = $ this-> db-> getAll ($ sql2); foreach ($ result2 as $ k => $ v) {$ result2 [$ k] ['flat'] = 2;} $ result = array_merge_recursive ($ result1, $ result2 );

$sql1= "select id,1 as flat from table1 where title like '%$keyword%' union all select id,2 as flat from table2 where title like '%$keyword%'";


In this way, you can execute a query, and the following merged arrays can be avoided.

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.