Home >Backend Development >PHP Tutorial > 求解一条sql解决办法

求解一条sql解决办法

WBOY
WBOYOriginal
2016-06-13 13:31:48743browse

求解一条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 =_=...

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn