mysql查询组并显示可能的值

时间:2015-05-12 22:05:40

标签: mysql sql

我正在寻找一个更好的,但无法在互联网上找到它(maby,因为我不知道正确的术语)。我正在搜索对我的值进行分组的查询,并为预定义值显示零。

我的表的值为0 - 5(例如1,0,4,4,0,0,4,2,1,5,0)

我需要一个始终显示数字的查询(也是零值):

nr |量
0 | 4
1 | 2
2 | 1
3 | 0
4 | 3
5 | 1

这可能吗?

Greets,Len

2 个答案:

答案 0 :(得分:2)

Here is what you need. This assumes that you know up-front that the values 0-5 are the only possible values that can be present in your table. If other values are present, that's OK but you won't count them.

ScriptsAndPrograms

SQL Fiddle here:

http://sqlfiddle.com/#!9/bc704f/12

答案 1 :(得分:0)

Create a numbers table with single column "num" and insert all your numbers (0-5).

Then do a left join query and group by the num like this:

XmlSerializer serializer = new XmlSerializer(typeof(Incident));
using (StringReader reader = new StringReader(xmlDocumentText))
{
    Incident incident= (Incident)(serializer.Deserialize(reader));
}