Mysql query on 5 tables
I have searched google for 2 hours and no luck on what I think is simple. I have 4 tables that I can union together the first 4 below and I have all the rows from all the tables. I am trying to figure out ho开发者_StackOverfloww I can add 1 extra column of FullName so that all the results show the FullName from table5 in every row based on b which is in all the other tables. I hope this makes sense to someone.
SELECT a, b, NULL AS Name FROM table1
union SELECT a, b, NULL AS Name FROM table2
union SELECT a, b, NULL AS Name FROM table3
union SELECT a, b, NULL AS Name FROM table4
union SELECT NULL AS a, b, FullName FROM table5 where
.....b is equal to any b in the previous 4 select statements
SELECT unions.*, tb5.FullName
FROM (
SELECT a, b FROM table1
union SELECT a, b FROM table2
union SELECT a, b FROM table3
union SELECT a, b FROM table4
union SELECT a, b FROM table5
) AS unions
JOIN table5 AS tb5 ON unions.b = tb5.b
Might work?
EDIT: Updated...
精彩评论