求解一條sql解決辦法

來源:互聯網
上載者:User
求解一條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 =_=...
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.