Let’s assume I have three table – list,a and b
table list
|a_id|--|b_id|--'name'
|4 |--|3 |--|foo|
|1 |--|2 |--|foo|
table a
|id|--|name|--|order|
|1 |--|a_1 |--|1 |
|2 |--|a_2 |--|2 |
.....................
|n |--|a_n |--|n |
table b
|id|--|name|--|order|
|1 |--|b_1 |--|1 |
|2 |--|b_2 |--|2 |
.....................
|n |--|b_n |--|n |
a_id in table list is id from a table, and
b_id in table list is id from b table
I need to get the name list from a and b tables ordered by their order, where name = ‘foo’ in list table.
ie. i need to get a_1,a_4 from table a, and b_2,b_3 from b.
I wrote a query like
SELECT
`a_1_table`.`name` a_1_name,
`b_1_table`.`name` b_1_name
FROM
`list`
LEFT JOIN
`a_1` AS a_1_table ON ( `a_1_table`.`id` = `list`.`a_id` )
LEFT JOIN
`b_1` AS b_1_table ON ( `b_1_table`.`id` = `list`.`b_id` )
WHERE
`list`.`name` = 'foo'
but as I see, in such structure, I can’t write order by .... ie. I can’ but I will not get what I want.
What can I do?
Use:
UNION ALLis faster thanUNION, because it doesn’t remove duplicates (whichUNIONdoes).