MySQL查询三个表

时间:2013-02-03 17:08:26

标签: mysql sql join

我有三个人员表,属性和属性的潜在价值。我无法找出一个查询来显示所有人,每个人属性及其缺失/ null属性。

这是一个示例表......

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我会使用WHERE语句(SQL Basics)连接这三个表。

你在发帖前做过搜索吗?

对于null,此帖子MySQL Show Null Results in Query - With INNER JOIN或此Mysql join four tables and show NULL value

- )

答案 2 :(得分: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

答案 3 :(得分:0)

使用您期望的输出作为起点,您基本上希望将people的属性和不同人的交叉产品保持连接到people的(n内部)连接的结果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
;