4

我有三张桌子,上面有人物、属性和属性的潜在价值。我想不出一个查询来显示所有人、每个人的属性及其缺失/空属性。

这是一个示例表...

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  |
+---------------+----------------+-------+


如何加入三个表以显示这样的内容?

+-----------+----------+-----------------+--------+
| 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   |
+-----------+----------+-----------------+--------+
4

4 回答 4

3
SELECT  a.person_ID, 
        MAX(c.value_ID) value_ID, 
        b.attribute_name ,  
        MAX(c.value) Value
FROM    people a
        CROSS JOIN attributes b       
        LEFT JOIN `values` c
            ON  a.value_ID = c.value_ID AND
                b.attribute_name = c.attribute_name
GROUP BY a.person_ID, b.attribute_name
于 2013-02-03T17:24:55.253 回答
0

我会使用 WHERE 语句(SQL 基础知识)连接三个表。

发帖前有搜索过吗?

对于 null,有这篇文章MySQL Show Null Results in Query - With INNER JOIN或这个Mysql join 四张表并显示 NULL 值

;-)

于 2013-02-03T17:11:53.813 回答
0
select
    person_id,
    attributes.attribute_name as attribute_name,
    values.value as value
from people
cross join attributes
left join values on people.value_id=values.value_id
                and attributes.attribute_name=values.attribute_name
于 2013-02-03T17:15:49.857 回答
0

使用您的预期输出作为起点,您基本上希望将属性和不同人员的叉积people从左连接到 to 的(n 内)连接的people结果values。这就是您可以在 SQL 中编写脚本的方式(尤其是在 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
;
于 2013-02-03T18:27:18.910 回答