简体   繁体   中英

php mysql combine queries

If I have 2 mysql_query commands in a single php file, is their a way to combine them?

For example, I have:

$a=mysql_query(SELECT * FROM table1);
$b=mysql_query(SELECT id FROM table3);

but I want to combine them into a single mysql_query, would this be more efficient? would it be faster?

UNION should work ( MySQL Manual )

SELECT id FROM table1 UNION SELECT id FROM table3;

Edit: I see: You want all ("*") from table1. This is a little bit more difficult, but UNION may help also. However, you are really sure you want to do this? Is there any relationship beetween those two tables, or should this just be a kind of micro optimization?

multiple queries are not supported in mysql_query as descripted on php manual , so you can't combine both query in php mysql_query way Here is another good reference from php manual notes :

Executed with multiple queries at once, the mysql_query function will return a result only for the first query. The other queries will be executed as well, but you won't have a result for them.

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