The use of mysql distinct

Source: Internet
Author: User

The use of mysql distinct By The Way, mysql's DISTINCT keyword has a lot you can't think of. 1. when count does not repeat, it can be used, for example, select count (DISTINCT id) FROM tablename; it is used to calculate the number of records with different IDs in the talbebname table, you can use select distinct id FROM tablename to return the specific values of different IDs in the talbebname table. in case 2 above, there will be ambiguity when you need to return results with more than two columns in the mysql table, such as select distinct id and type FROM tablename; in fact, the returned results are results with different IDs and types, that is, DISTINCT applies to two fields at the same time, so that the id and tyoe must be the same to be excluded. This is different from the expected result 4. at this time, you can consider using the group_concat function for troubleshooting, but this mysql function is supported above mysql4.1. another solution is to use SELECT id, type, count (DISTINCT id) FROM tablename although this returned result contains a column of useless count data (maybe you need this useless data I mentioned) the returned result is that only the results with different IDS can be used in concert with the above four types. It depends on what data you need.

Related Article

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.