当前位置: 代码迷 >> Sql Server >> mysql 行转列有关问题,求大神
  详细解决方案

mysql 行转列有关问题,求大神

热度:99   发布时间:2016-04-24 08:49:57.0
mysql 行转列问题,求大神


如图,一个小小的问题,难倒了几百人,求大神解救。
------解决思路----------------------
既然你都有条件了,为啥还要纠结行列呢。 直接查询出来不就OK了。

select t1.[1], t2.[2] 
from 
(select count(vote) as [1] from userlogin_info where vote = 1) as t1,
(select count(vote) as [2] from userlogin_info where vote = 2) as t2

------解决思路----------------------
看到你在说行列转换, 代码还截个图,想拷贝出来用都没法,所以沒看你代码, 以为你是要將列的结果转成行表示。。。

按照你的语句,那就这么写:

select SUM(case vote when 1 then 1 else 0 end) [1],
sum(case vote when 2 then 1 else 0 end) [2]  from userlogin_info 
where vote in(1,2)

用求和, 不需要再分组了。
  相关解决方案