简体   繁体   中英

How to select more than one database tables using one query

I want to displaying the value from more than one database mysql tables.

I have code like this :

$db1 = "SELECT * FROM db1 where no='1' ";
$db2 = "SELECT * FROM db2 where no='1' ";
$db3 = "SELECT * FROM db3 where no='1' ";  

and I want to use one query like this :

$sql = mysqli_query($connect, $db1);
while ($data = mysqli_fetch_array($sql)) 
   {
    bla bla bla...       
   }

is this possible if I used one query like that or there is any query to get example 3 database tables into one query? Thanks.

Nope you can not get data from multiple tables using one sql query without performing a join on them.

But you can use mysqli_multi_query to execute multiple queries.

Please check https://www.w3schools.com/php/func_mysqli_multi_query.asp

I am assuming you have three different databases in the same server. In this case, you can write the query in the below format Database.Schema.Table So in your case

$db1 = "SELECT * FROM Database.Schema.db1 where no='1' ";
$db2 = "SELECT * FROM Database.Schema.db2 where no='1' ";
$db3 = "SELECT * FROM Database.Schema.db3 where no='1' ";

Hope this is what you are looking for!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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