A SQL solution

Source: Internet
Author: User
To solve an SQL statement, assume that three tables tata_id (primary key) exist (primary key) tb_taidtb_cont & nbsp; tctc_id (primary key) tc_tatypeidtc_type. where tb_taid corresponds to ta_id and tc_tatypeid
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 = _ =...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.