MySQL query for three tables
I have three tables with people, attributes and potential value for attributes. I can't seem to find a query to display all people, each person's attributes and their missing / null attributes.
Here's an example table ...
attributes
+---------------------+
| attribute_name (col)|
+---------------------+
| name |
+---------------------+
| age |
+---------------------+
| gender |
+---------------------+
| email |
+---------------------+
people
+-----------+----------+
| person_id | value_id |
+-----------+----------+
| 2 | 7 |
+-----------+----------+
| 2 | 9 |
+-----------+----------+
| 3 | 8 |
+-----------+----------+
values
+---------------+----------------+-------+
| value_id (pk) | attribute_name | value |
+---------------+----------------+-------+
| 7 | age | 35 |
+---------------+----------------+-------+
| 8 | age | 28 |
+---------------+----------------+-------+
| 9 | gender | male |
+---------------+----------------+-------+
How do I join three tables to display something like this?
+-----------+----------+-----------------+--------+
| person_id | value_id | attribute_name | value |
+-----------+----------+-----------------+--------+
| 2 | 7 | age | 35 |
+-----------+----------+-----------------+--------+
| 2 | 9 | gender | male |
+-----------+----------+-----------------+--------+
| 2 | NULL | name | NULL |
+-----------+----------+-----------------+--------+
| 2 | NULL | email | NULL |
+-----------+----------+-----------------+--------+
| 3 | 8 | age | 28 |
+-----------+----------+-----------------+--------+
| 3 | NULL | gender | NULL |
+-----------+----------+-----------------+--------+
| 3 | NULL | name | NULL |
+-----------+----------+-----------------+--------+
| 3 | NULL | email | NULL |
+-----------+----------+-----------------+--------+
source to share
I would do a join of three tables with a WHERE clause (SQL basics).
Did you do a search before posting?
For null, this is the post MySQL Show Null Results in Query - with INNER JOIN or this Mysql joins four tables and show NULL
; -)
source to share
Using your expected result as a starting point, you essentially want the cross product of attributes and individuals from to people
remain connected to the result of the (n inner) join people
- values
. And this is how you could script that in SQL (especially in MySQL):
SELECT
p.person_id,
v.value_id,
a.attribute_name,
v.value
FROM
(SELECT DISTINCT person_id FROM people) p
CROSS JOIN attributes a
LEFT JOIN
people d
INNER JOIN `values` v ON d.value_id = v.value_id
ON p.person_id = d.person_id
AND a.attribute_name = v.attribute_name
;
source to share