在mysql中执行完全外连接

时间:2015-09-09 05:49:08

标签: mysql join

我已经知道我可以使用union在MySQL中进行外连接。 我也检查一下。 Full Outer Join in MySQL

但是我想做这样的事情并且我不知道如何使用联合来实现这一目标。

我有db table user_count如下。

+-------------+-----------+---------+-------+
| meb_id      | left_id   |right_id |active |    
+-------------+-----------+---------+-------+
| 1001        | (NULL)    | (NULL)  |  1    | 
| 1002        | 1001      | 0       |  0    | 
| 1003        | 0         | 1001    |  0    |
| 1004        | 1001      | 0       |  0    |
| 1004        | 1002      | 0       |  0    |
+-------------+-----------+---------+-------+

我的查询如下。

    SELECT left_id, COUNT(left_id) AS left_count FROM `user_count` GROUP BY left_id 

    SELECT right_id, COUNT(right_id) AS right_count FROM `user_count` GROUP BY right_id;

SELECT left_id AS meb_id, COUNT(left_id) AS active_left_count FROM `user_count` WHERE active = 1 GROUP BY left_id;

SELECT right_id AS meb_id, COUNT(right_id) AS active_right_count FROM `user_count` WHERE active = 1 GROUP BY right_id;

我想预先形成外连接或联合,所以我的结果将是这样的

+-------------+-----------+------------+------------+--------------+
| meb_id      |left_count |right_count |active_left |active_right  | 
+-------------+-----------+------------+------------+--------------+
| (NULL)      | 0         | 0          |  0         | 0            |
| 0           | 1         | 3          |  0         | 0            |
| 1001        | 2         | 1          |  0         | 0            | 
| 1002        | 1         | 0          |  0         | 0            |
| 1003        | 0         | 0          |  0         | 0            | 
+-------------+-----------+------------+------------+--------------+

我该怎么办?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

...
    if(c.GetType() == typeof(string))
        return "Hurray c and b are strings" + b;
}
return null;

<强>演示 sqlfiddle