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.