Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of distinct in mysql

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about the role of distinct in mysql, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1. Can be used when count records are not duplicated.

For example, SELECT COUNT (DISTINCT id) FROM tablename

Is to calculate the number of different id records in the talbebname table.

2. When you need to return specific values of different id records, you can use the

Like SELECT DISTINCT id FROM tablename.

Returns the specific values of different id in the talbebname table

3. Case 2 above is ambiguous when you need to return results with more than 2 columns in the mysql table.

Like SELECT DISTINCT id, type FROM tablename.

In fact, what is returned is the result that id and type are different at the same time, that is, DISTINCT acts on two fields at the same time, and it must be excluded if both id and tyoe are the same, which is different from the result we expected.

4. At this point, you can consider using the group_concat function to exclude, but this mysql function is only supported above mysql4.1.

5. In fact, there is another solution, which is to use the

SELECT id, type, count (DISTINCT id) FROM tablename

Although this return result has an extra column of useless count data (maybe you need what I call useless data)

The result returned is that only all the different results of id and the above four types can complement each other, depending on what kind of data you need.

This is what the role of distinct in mysql is, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report