SQL Example 9--group, having

Source: Internet
Author: User

<title>SQL Example 9–group, having</title> SQL Example 9–group, having
  • Group
      • SQL Create
      • SQL Delete
      • SQL Select
    TruncateTableUser1;Insert  intoUser1 (ID, username)Values(1,' test1 ');Insert  intoUser1 (ID, username)Values(2,' test1 ');Insert  intoUser1 (ID, username)Values(3,' test1 ');Insert  intoUser1 (ID, username)Values(4,' test1 ');Insert  intoUser1 (ID, username)Values(5,' test1 ');Insert  intoUser1 (ID, username)Values(1,' Test2 ');Insert  intoUser1 (ID, username)Values(2,' Test2 ');Insert  intoUser1 (ID, username)Values(3,' Test2 ');Insert  intoUser1 (ID, username)Values(4,' Test2 ');Insert  intoUser1 (ID, username)Values(5,' Test2 ');Insert  intoUser1 (ID, username)Values(1,' Test3 ');Insert  intoUser1 (ID, username)Values(2,' Test3 ');Insert  intoUser1 (ID, username)Values(3,' Test3 ');Insert  intoUser1 (ID, username)Values(4,' Test3 ');Insert  intoUser1 (ID, username)Values(5,' Test3 ');
    Select  from Group  by username;
    | ID | Username |+----+----------+|  1 | Test1    | |  1 | Test2    | |  1 | Test3    |
Select  from Group  by ID;
| ID | Username |+----+----------+|  1 | Test1    | |  2 | Test1    | |  3 | Test1    | |  4 | Test1    | |  5 | Test1    |

Grouping is actually asking for a collection?

Select  from Group  by 1;

1 refers to the username, group by 2 refers to the ID (generally not used, good ugly usage)

  • Having
    Equivalent to where
    Select  from Group  by ID;
    | Username | ID |+----------+----+| Test1    |  1 | | Test1    |  2 | | Test1    |  3 | | Test1    |  4 | | Test1    |  5 |
    Select  from Group  by  having inch (1, 2, 3);
    | Username | ID |+----------+----+| Test1    |  1 | | Test1    |  2 | | Test1    |  3 |
    Select  from Group  by  having inch (1, 2, 3);
    Select  from Group  by  having inch (1, 2, 3);   # error, no ID this field

SQL Example 9--group, 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.