Farklı sütunları ile birden fazla tablodan sonuçlarını birleştirmek için nasıl?

0 Cevap php

Ben farklı numaralar ve sütunların türleri ve ortak tek bir sütun ile çeşitli tablolar var.

+--------+---------+------------+-------------+
| person | beardID | beardStyle | beardLength |
+--------+---------+------------+-------------+

+--------+-------------+----------------+
| person | moustacheID | moustacheStyle |
+--------+-------------+----------------+

Ben paylaşılan sütunun verilen değeri eşleşen tüm sonuçlar almak istiyoruz. Ben bu gibi birden çok select deyimleri kullanarak yapabilirsiniz:

SELECT * FROM beards WHERE person = "bob"

ve

SELECT * FROM moustaches WHERE person = "bob"

But this requires multiple 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 ve similar type of columns. I could write a SELECT statement that would manually pad the results from each table by adding columns with NULL values, but that would quickly get unmanageable for a few more tables with a few more columns.

Ben kabaca böyle bir sonuç kümesi için arıyorum:

+--------+---------+------------+-------------+-------------+----------------+
| 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's fast ve maintainable? Or am I better off running a separate query for each table?

Clarification:

I'm not looking for a cartesian product. I don't want a row for every combination of beard-ve-moustache, I want a row for every beard ve a row for every moustache.

So if there are 3 matching beards ve 2 matching moustaches I should get 5 rows, not 6.

0 Cevap