当前位置: 代码迷 >> Sql Server >> 请问一个查询的SQL,弄了半天弄不出来。来
  详细解决方案

请问一个查询的SQL,弄了半天弄不出来。来

热度:76   发布时间:2016-04-27 17:59:33.0
请教一个查询的SQL,弄了半天弄不出来。来请教高手
请教一个查询的SQL,弄了半天弄不出来。来请教高手
SQL code
tbaid Savepath1,  xxx  2,  xxx  3,  xxx  4,  xxx  5,  xxx  tbbid,aid11,112,413,414,5--想要的结果id, Savepath,count(指tbb的记录条数)1,  xxx    ,1   2,  xxx    ,0   3,  xxx    ,0   4,  xxx    ,2  5,  xxx    ,1  (下面的总出错因为Savepath是TEXT型,总是出错:不能比较或排序 text、ntext 和 image 数据类型,除非使用 IS NULL 或 LIKE 运算符错误)select a.id,a.savepath,count(b.aid) from tba ainner join tbb b on b.aid=a.idgroup by a.id,a.savepath,b.count


------解决方案--------------------
SQL code
create table tba(id int,Savepath text)insert into tba select 1,  'xxx'  insert into tba select 2,  'xxx'  insert into tba select 3,  'xxx'  insert into tba select 4,  'xxx'  insert into tba select 5,  'xxx'  create table tbb(id int,aid int)insert into tbb select 11,1insert into tbb select 12,4insert into tbb select 13,4insert into tbb select 14,5goselect *,(select count(*) from tbb where aid=a.id)ctfrom tba aid          Savepath             ct----------- ----------------- -----------1           xxx                  12           xxx                  03           xxx                  04           xxx                  25           xxx                  1(5 行受影响)godrop table tba,tbb
------解决方案--------------------
探讨
5楼那样会和我同样出错的,Savepath是TEXT型不能那样GROUP BY

------解决方案--------------------
探讨

我想用5楼这样的,主要是解决TEXT型GROUP BY出错就可以,谢谢
SQL code
select a.ID,a.Savepath,count(b.ID) as con from tba as a left join tbb as b on a.ID=b.aid
group by a.ID,a.Savepath
  相关解决方案