查询a,b,c三个表,a表有字段id,c表有aid是外键到a表,c表haiyoudt时间字段和state字段,正常情况下我根据state查询出来的只有yigedt字段,但是我现在想根据state3个状态查询3个dt字段的值,显示在3列
解决方案 »
- 比较困惑的sqlserver holdlock锁,懂锁的进来
- SQL能实现{将A表中的所有数据复制到B表(B表含有A表中所有字段,只是部分字段名称不同,但增加了5个新的字段,且新增的5个字段被设置为“不能为空”)}吗?
- 怎么查询出总金额?
- "SET XACT_ABORT OFF"为什么不管用
- 执行m_pCommand->Execute()老出现异常,不知怎么回事?
- 位置固定的模糊查询怎么写?
- 我的要求就是 1.大数据量千万级别的用户
- 如何在sql语句里禁止sql server自动使用高速缓存
- MS SQL Server 2000企业版安装后,为何里面没有例子数据库?
- 数据库存储文件
- WITH 用法求教
- 发布订阅问题求解
max(case 状态 when 'A' thenT.状态 else '' end) A,
max(case 状态 when 'B' then T.状态 else '' end) B,
max(case 状态 when 'C' then T.状态 else '' end) C
from tb
left join [sign] T on T.id = tb.id --外链的表
group by 时间类似这样。
max(case c.state when 'A' then c.state else '' end) A,
max(case c.state when 'B' then c.state else '' end) B,
max(case c.state when 'C' then c.state else '' end) C
from a
left join c on c.aid = a.id --外链的表
where c.state = '要查的类型'
group by c.haiyoudt
--A B C 代替你要显示为列的值,可以自己扩展一下
现在这样只能查询state一种状态,我怎么才能同时查询3种状态呢?
max(case c.state when 'A' then c.state else '' end) A,
max(case c.state when 'B' then c.state else '' end) B,
max(case c.state when 'C' then c.state else '' end) C
from a
left join c on c.aid = a.id --外链的表
where c.haiyoudt >'2014-01-01' --设定时间查找即可
group by c.haiyoudt
max(case c.state when 'A' then c.state else '' end) A,
max(case c.state when 'B' then c.state else '' end) B,
max(case c.state when 'C' then c.state else '' end) C,
sum(case when c.state in ('A','B') then c.state else '' end) as 'AB sum'
from a
left join c on c.aid = a.id --外链的表
where c.haiyoudt >'2014-01-01' --设定时间查找即可
group by c.haiyoudt参考