Problem about distinct in mysql query statements

Source: Internet
Author: User
Tags mysql manual

Mysql query statements are often used. Today, the following requirements occur when you maintain the database. mysql query statements are used to find records that are not repeated in the user table and use distinct, but they can only be used for one field, I tried it many times. What should I do?

The reason is that distinct is used to return the number of records that do not repeat, instead of using it to return all values that do not record duplication.
That is, distinct can only return its target field, but cannot return other fields.
For example:

 
 
  1. SELECT   DISTINCT mac,ip from ip  
  2. +------+------+  
  3. | mac   | ip |  
  4. +------+------+  
  5. | abc   |   678 |   
  6. | abc   |   123 |   
  7. | def   |   456 |   
  8. | abc   | 12 |   
  9. +------+------+  

He still won't change! Because the preceding statement serves two fields, that is, the mac and ip addresses must be the same to be excluded.

Finally, there is no way to use group !!!!
View mysql manual! Connt (distinct name) can be implemented with group.
A count function is used to implement the functions I want.

 
 
  1. select *,count(distinct mac) from ip group by mac;  
  2. +------+------+---------------------+  
  3. | mac   | ip | count(distinct mac) |  
  4. +------+------+---------------------+  
  5. | abc   |   678 |                1 |   
  6. | def   |   456 |                1 |   
  7. +------+------+---------------------+  

Basically implement my ideas!

So how to implement a table with two fields mac and ip, and how to find records with the same mac and different ip addresses?

 
 
  1. mysql> select * from ip;  
  2. +-----+-----+  
  3. | mac | ip   |  
  4. +-----+-----+  
  5. | abc | 123 |  
  6. | def | 456 |  
  7. | ghi | 245 |  
  8. | abc | 678 |  
  9. | def | 864 |  
  10. | abc | 123 |  
  11. | ghi | 245 |  
  12. +-----+-----+  
  13. 7 rows in set (0.00 sec)  
  14.  
  15. mysql> SELECT   DISTINCT a.mac, a.ip  
  16. -> FROM ip a, ip b  
  17. -> WHERE a.mac = b.mac AND a.ip <> b.ip ORDER BY a.mac;  
  18. +-----+-----+  
  19. | mac | ip   |  
  20. +-----+-----+  
  21. | abc | 678 |  
  22. | abc | 123 |  
  23. | def | 864 |  
  24. | def | 456 |  
  25. +-----+-----+  
  26. 4 rows in set (0.00 sec)  
  27.  

How to Implement MySQL full-text Query

Optimization of MySQL query Paging

MySQL query results are sorted by a certain value

Use functions to query row numbers in MySQL

Non-empty question in MySQL Query

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.