Assume that the following three tables tata_id (primary key) exist (primary key) tb_taidtb_cont & nbsp; tctc_id (primary key) tc_tatypeidtc_type. tb_taid corresponds to ta_id and tc_tatypeid, now an SQL statement is taken out to solve an SQL statement.
Suppose there are three tables below
Ta
Ta_id (primary key) ta_name ta_cont ta_typeid
Tb
Tb_id (primary key) tb_taid tb_cont
Tc
Tc_id (primary key) tc_tatypeid tc_type
Where, tb_taid corresponds to ta_id, tc_tatypeid corresponds to ta_typeid, and now one SQL statement extracts ta_name, ta_cont, count (tb_taid), how to write tc_type
I wrote this statement but it is not correct. count (tb_taid) as com is used to retrieve the count of all IDs instead of the count of each ta_id. how can I write it?
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;
------ Solution --------------------
If you do not specify the group key, you will naturally not get the correct result.
------ Solution --------------------
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;
Such a name will confuse people. In short, use group by = _ =...