简体   繁体   English

PHP MySQL两个查询

[英]PHP MySQL Two queries

I would like to Know how can I in PHP code, execute two mysql query (query_1 and query_2) but display only the results of query_2 for each result of query_1 like that : 我想知道如何在PHP代码中执行两个mysql查询(query_1和query_2),但仅对query_1的每个结果显示query_2的结果,如下所示:

query_1==> `SELECT tbl_name FROM table_ref `

query_2 ==> `SELECT id,name FROM (result of the first query)`

query_1 return the names of tables : table1, table2, table3, and with the query_2 I have to do this : query_1返回表的名称:table1,table2,table3,并且使用query_2我必须这样做:

SELECT id,name FROM table1 and SELECT id,name FROM table2 ... as in a loop SELECT id,name FROM table1SELECT id,name FROM table2 ...

Thanks in advance for your advice ! 在此先感谢您的建议!

SELECT id, name FROM sometable WHERE name in (SELECT tbl_name FROM table_ref)

在此示例中,两个表之间需要建立连接

On php side, you can deal with both the aray results as foll: 在php方面,您可以将以下两种结果都作为处理:

$array1 = array('tbl_name1' => '' ,'tbl_name2' => '');

$array2 = array('tbl_name1' => array('id' => 'id1'),
                'tbl_name2' => array('id' => 'id2'));

echo '<pre>';
print_r(array_intersect_key($array2, $array1));
echo '</pre>';

o/p: O / P:

Array
(
    [tbl_name1] => Array
        (
            [id] => id1
        )

    [tbl_name2] => Array
        (
            [id] => id2
        )

)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM