简体   繁体   中英

Trouble using UNION ALL

I'm using the following to get data from database in a php file:

$query_pedVR = sprintf("SELECT * FROM tablename WHERE active = 1 ORDER BY name ASC");

What I need to do is combine two tables in one query but can't seem to get it to work. I have tried the UNION ALL, but can't find the syntax that works. This isn't working:

$query_pedVR = sprintf("SELECT * FROM table1 UNION ALL SELECT * FROM table2 WHERE active = 1 ORDER BY name ASC");

Also tried:

$query_pedVR = sprintf("SELECT * FROM table1 WHERE active = 1 ORDER BY name ASC UNION ALL SELECT * FROM table2");

There are not any common entries in the two tables. The two tables also have a different set of columns.

Is it possible to do this with different table structures?

Thanks for any help you can offer.

If tables have different structure you can't use select * but you must specify single fields

SELECT field1,field2 FROM table1 WHERE active = 1
union all
SELECT field1,field2 FROM table2 WHERE active = 1

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