Query data with the same field values in a table

Source: Internet
Author: User

To query data with the same field values in a table.

 

Example:

 

For example, the following table is available: person

 

We can see that the values of name and city are the same in the data rows of ID = 1 and ID = 5, id = 2 and ID = 6. How can we query it?

 

Take a look at the following SQL statement:

 

Select p1.name, p1.city from person P1, person P2 where p1.name = p2.name and p1.city = p2.city;

 

This statement uses its own link to query a single table. But the result is as follows:

We can see that this is not the expected result. The error is that the same data row in a table is compared to meet the condition. Therefore, it is output.

 

 

Therefore, you can use the following solution:

 

Select p1.name, p1.city from person P1, person P2 where p1.name = p2.name and p1.city = p2.city and p1.id! = P2.id;

 

Let's take a look at the results:

 

 

Meet our requirements. This method can be expanded to 1 field, 2 fields, 3 fields, or even more.

 

In some software systems, two or three software systems are not allowed... If the fields are the same, you can use the above method to query and delete useless data.

 

 

 

 

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.