Heim  >  Artikel  >  Backend-Entwicklung  >  求解一条sql解决办法

求解一条sql解决办法

WBOY
WBOYOriginal
2016-06-13 13:31:48691Durchsuche

求解一条sql
假设有下边的三张表
ta
ta_id(主键) ta_name ta_cont ta_typeid

tb
tb_id(主键) tb_taid tb_cont
 
tc
tc_id(主键) tc_tatypeid tc_type

其中,tb_taid对应ta_id,tc_tatypeid对应ta_typeid,现在一条sql取出 ta_name,ta_cont,count(tb_taid),tc_type怎么写

我是这样写的 但是不对,count(tb_taid) as com这句去取出了所有id的count,而不是每个ta_id的count,请问该怎么写

select ta_name,ta_cont,count(tb_taid) as com,tc_type from ta left join tb on ta_id=tb_taid left join tc on ta_typeid=tc_tatypeid order by ta_id desc;

------解决方案--------------------
你没有指定分组键,自然就得不到正确的结果
------解决方案--------------------

select ta_name,ta_cont,count(tb_taid) as com,tc_type from ta left join tb on ta_id=tb_taid left join tc on ta_typeid=tc_tatypeid GROUP BY ta_cont order by ta_id desc;

这样的的命名把人看晕了。总之用用GROUP BY =_=...

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn