I have 4 table, default_table, table_a, table_b, table_c, each have two columns,
I want to select name and id from all table then sort it,
I try"SELECT Name,ID FROM default_table, table_a, table_b, table_c ORDER BY Name"
but this give me error, so can someone help me with this problem, i need code in sql or php, or even javascript if it can
I have 4 table, default_table, table_a, table_b, table_c, each have two columns, I want
Share
Since
order byis applied after unioning, you can just do:Your problem is that you’re doing a Cartesian product of all the table data so it’s can’t figure out which
idornameyou mean. Even if it could, you’d get way more rows than you’d expect, due to the multiplicative effect. You want to join the data simply by adding all the rows to the output.Explaining the distinction further, let’s say you have two tables:
The query:
(the cartesian product) will give you:
(the row count is the product of the individual row counts) whereas:
(the union) will give you:
(the row count is the sum of the individual row counts).