I have three tables with people, the attributes and potential value for the attributes. I can’t figure out a query to display all the people, each persons 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 the 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 |
+-----------+----------+-----------------+--------+
1 Answer