I have several tables with different numbers and types of columns and one column.
+--------+---------+------------+-------------+ | person | beardID | beardStyle | beardLength | +--------+---------+------------+-------------+ +--------+-------------+----------------+ | person | moustacheID | moustacheStyle | +--------+-------------+----------------+
I want to get all the results that match the given value of a common column. I can do this using several select statements, for example:
SELECT * FROM beards WHERE person = "bob"
and
SELECT * FROM moustaches WHERE person = "bob"
But this requires several mysql API calls, which seems inefficient. I was hoping I could use UNION ALL to get all the results in a single API call, but UNION requires that the tables have the same number and the same type of columns. I could write a SELECT statement that would manually put the results from each table, adding columns with NULL values, but that would quickly become unmanageable for multiple tables with multiple columns.
I am looking for a result set something like this:
+--------+---------+------------+-------------+-------------+----------------+ | person | beardID | beardStyle | beardLength | moustacheID | moustacheStyle | +--------+---------+------------+-------------+-------------+----------------+ | bob | 1 | rasputin | 1 | | | +--------+---------+------------+-------------+-------------+----------------+ | bob | 2 | samson | 12 | | | +--------+---------+------------+-------------+-------------+----------------+ | bob | | | | 1 | fu manchu | +--------+---------+------------+-------------+-------------+----------------+
Is there a way to achieve this that is fast and easy to maintain? Or am I better off running a separate query for each table?
Clarification:
I am not looking for a delicate product. I do not need a string for each combination of beards and mustaches, I need a string for each beard and a string for each mustache.
So, if there are 3 matching beards and 2 matching mustaches, I should get 5 lines, not 6.
join php mysql union
Robert
source share