distinct and group by deduplication

   mysql commonly used method is to duplicate data or use distinct group by, 2 or more kinds can achieve, but there are two different places.

distinct features:

Such as: select distinct name, sex, from tb_students the sql syntax, the query tb_students table name, sex, and remove duplicate names and gender of all students:

  1, distinct query field only on the front, middle, or can not be placed behind the query fields.

Note: select sex, distinct name from tb_students such an approach is wrong, distinct only write in front of all the query fields

  2, the rear of all distinct fields each function, i.e., to re-query all fields complete duplication of data, instead of just a single field data connection distinct later repeated.

Note: that is distinct keyword name, sex all play a role, to re-name, sex exactly the same student, if the same name, gender difference is not going heavy.

  3, to query multiple fields, but only for a field to heavy, heavy to use distinct words can not be achieved.

 

group by Features:

  1, generally using clustering function (e.g., count () / sum (), etc.), can also be used alone.

  2, were also group by acting on the back of all the fields, i.e., to re-query all fields complete duplication of data, instead of just a single field data group by repeating the rear of the connector.

  3, the rear group by Field of the query packet is not limited.

Special note: when used in the group by the oracle, the query must be a field in the packet group by clustering fields and functions. Such as select name, sex from tb_students group by name this sql

    In oracle syntax is wrong, because sex is not behind the group by group; but in mysql is supported.

 

Guess you like

Origin www.cnblogs.com/whitemouseV2-0/p/11226444.html