SQL Note 1,left Join,group by,having

Source: Internet
Author: User

Table: XS,XK,CJ

Left JOIN table 1 on table 1. field = Table 2. Fields

GROUP BY group conditions

ORDER BY sort conditional ASC Positive order (small to large), DESC reverse

You have a condition like where, but you need to include the field in the Return set field

Otherwise error: Unknown column ' C.CJ ' in ' have clause ', unknown columns, having

Cases:

#查询有不及格的成绩的学生姓名
SELECT
S.XSMC, C.CJ
From CJ C
Left JOIN xs s on S.XSDM=C.XSDM
Left JOIN xk k on K.XKDM=C.XKDM
GROUP by C.XSDM
Having C.CJ <60

#查询所有学生所有学科的成绩 <=60
SELECT
S.xsmc,k.xkmc,c.cj
From XS S,xk K,CJ c
WHERE S.XSDM = C.XSDM
and K.XKDM = C.XKDM
and C.CJ <= 60


#查询学生总成绩
#ASC正序小到大, Desc is large to small in reverse
SELECT S.XSMC, SUM (C.CJ)
From XS S,xk K,CJ c

WHERE S.XSDM = C.XSDM
and K.XKDM = C.XKDM
GROUP by C.XSDM
ORDER by
SUM (C.CJ) ASC

SQL Note 1,left Join,group by,having

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.