1

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 :

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 :

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

Thanks in advance for your advice !

1
  • Hope tbl_name is not tablename :) Not getting crazy :) Commented Apr 12, 2013 at 10:09

2 Answers 2

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

In this example there need to be a connection between 2 tables

Sign up to request clarification or add additional context in comments.

1 Comment

yeah but that's not quite what I need, because sometable comes from the second query which follow IN in your code
0

On php side, you can deal with both the aray results as foll:

$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:

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

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

)

4 Comments

but how do i put the two queries together, especially that there is a lot of results
array1 => result of query1, array2 => result of query2
I don't see where do you include query1 in query2, I need something like : SELECT id,name FROM (SELECT tbl_name FROM table_ref )
quer1 return the names of tables : table1, table2, table3, and with the query2 I have to do this : SELECT id,name FROM table1 and SELECT id,name FROM table2and ... as in a loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.