按ID分组并合并数据,其中为null

时间:2016-11-12 17:36:55

标签: mysql

我已加入多表并获得此结果

project_id | title|phone   | office_phone 
+++++++++++++++++++++++++++++++++++++++
3          |a     |1       |NULL
4          |b     |Null    |1   
4          |b     |2       |Null
5          |c     |Null    |1
5          |c     |Null    |2
5          |c     |Null    |3
5          |c     |1       |Null  
5          |c     |2       |Null    

现在我希望按项目ID分组,并将数据合并为null

我想要这样的输出

project_id | title|phone   | office_phone 
+++++++++++++++++++++++++++++++++++++++
3          |a     |1       |NULL
4          |b     |2       |1   
5          |c     |1       |1
5          |c     |1       |2
5          |c     |1       |3
5          |c     |2       |1
5          |c     |2       |2
5          |c     |2       |3

我该怎么做?

1 个答案:

答案 0 :(得分:0)

您需要完全外部自联接,但由于MySql不支持<a href="{% url 'polls:results' choice.question_id choice.choice_test%}">{{ choice.choice_test }}</a> 语法,因此您必须恢复为full outer join才能获得相同的结果:

union

sqlfiddle上看到它。

相关问题