1

我有一个问题。这对你来说可能很容易。

我正在尝试构建一个 d3js 和弦图 - 像这样(http://bl.ocks.org/4062006):

在此处输入图像描述

但是,我正在从我的 mysql 数据库中获取数据

我的表如下所示:

id gender_taker gender_giver
1     F            M
2     M            M
3     F            M
4     F            F

我希望输出看起来像这样:

gender_giver gender_taker count(*)
M            F            2
M            M            1
F            F            1

这很容易,可以通过以下方式产生:

SELECT gender_giver, gender_taker, COUNT(*) FROM data WHEREclauses GROUP BY gender_taker, gender_giver

但我还有另一个问题,我还有两个看起来像这样的表: 表 1:

id entryid gender_taker
1   2       F
2   2       M
3   3       F

表 2:

id entryid gender_giver
1   1       M
2   1       F
3   2       M

entryid 基本上是第一个表的 id,表明 Table2 和 Table3 只是 table1 的子集

如果将这三个表结合起来,它可能看起来像:

id gender_taker gender_giver
1     F            M,M,F
2     M,F,M        M,M
3     F,F          M
4     F            F

因此,对于和弦图,我希望将所有这些表格都考虑在内,最终给出如下内容:

gender_giver gender_taker count(*)
M            F            6           
M            M            4
F            F            2
F            M            0

请帮帮我。

4

1 回答 1

2
DROP TABLE IF EXISTS core;
CREATE TABLE core
(entry_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,gender_taker CHAR(1) NOT NULL
,gender_giver CHAR(1) NOT NULL
);

INSERT INTO core VALUES
(1     ,'F','M'),
(2,'M','M'),
(3,'F','M'),
(4,'F','F');

DROP TABLE IF EXISTS table1;
CREATE TABLE table1
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,entryid INT NOT NULL
,gender_taker CHAR(1) NOT NULL
);

INSERT INTO table1 VALUES
(1   ,2       ,'F'),
(2   ,2       ,'M'),
(3   ,3       ,'F');


DROP TABLE IF EXISTS table2;
CREATE TABLE table2
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,entryid INT NOT NULL
,gender_giver CHAR(1) NOT NULL
);

INSERT INTO table2 VALUES

(1   ,1       ,'M'),
(2   ,1       ,'F'),
(3   ,2       ,'M');

SELECT entry_id
     , GROUP_CONCAT(gender_taker) gender_takers
     , GROUP_CONCAT(gender_giver) gender_givers
  FROM 
     ( SELECT * FROM core
       UNION
       SELECT entryid,gender_taker,NULL FROM table1
       UNION
       SELECT entryid,NULL,gender_giver FROM table2
     ) x
 GROUP 
    BY entry_id;
+----------+---------------+---------------+
| entry_id | gender_takers | gender_givers |
+----------+---------------+---------------+
|        1 | F             | M,M,F         |
|        2 | M,F,M         | M,M           |
|        3 | F,F           | M             |
|        4 | F             | F             |
+----------+---------------+---------------+

SELECT a.gender taker
     , b.gender giver
     , COUNT(*)
  FROM 
     (
       SELECT entry_id,'taker' role, gender_taker gender FROM core
       UNION ALL
       SELECT entry_id,'giver', gender_giver FROM core
       UNION ALL
       SELECT entryid,'taker',gender_taker FROM table1
       UNION ALL
       SELECT entryid,'giver',gender_giver FROM table2
     ) a
  JOIN
     (
       SELECT entry_id,'taker' role, gender_taker gender FROM core
       UNION ALL
       SELECT entry_id,'giver', gender_giver FROM core
       UNION ALL
       SELECT entryid,'taker',gender_taker FROM table1
       UNION ALL
       SELECT entryid,'giver',gender_giver FROM table2
     ) b
    ON b.entry_id = a.entry_id
   AND b.role = 'giver'
   AND a.role = 'taker'
 GROUP 
    BY taker
     , giver;
+-------+-------+----------+
| taker | giver | COUNT(*) |
+-------+-------+----------+
| F     | F     |        2 |
| F     | M     |        6 |
| M     | M     |        4 |
+-------+-------+----------+
于 2013-02-01T10:32:48.083 回答