试图让不同的行的计数在一个查询中,并且在一个行SQL查询值
表1 ..
ID name
1 a
2 b
3 c
表2
ID Parent status
1 1 0
2 1 1
3 1 0
4 1 0
5 1 2
6 2 0
期望结果(不同子元素的计数)
ID name 0a 1s 2s
1 a 3(count of 0s) 1 (counts of 1s) 2 (count of 2s)
,我们可以得到这样一个查询中..
我曾尝试是导致我值3行
Select t1.id, t1.name, count(status) from TABLE_1 t1 Left JOIN TABLE_2 t2
ON t1.id = t2.parent
group by status
ID name status
1 a 3
1 a 1
1 a 1
2 b 1
你用什么? MySql或SqlServer - sql方言有不同的实现。看起来你可以在连接表上使用PIVOT:看看这里:https://stackoverflow.com/questions/13372276/simple-way-to-transpose-columns-and-rows-in-sql –
一旦你'我想到了你正在使用的RDBMS,实际上考虑处理应用程序代码中的数据显示问题。 – Strawberry
它的Mysql现在..认为它没有多大关系 – Luckyy