Querying all records with the same name in the database

Source: Internet
Author: User
A problem was encountered yesterday, querying all records in the user table that have the same user name. The problem at first glance is simple, but it's not that easy to think about. The first reaction of the mind is to read a piece of data after the loop looks for there is no same,
As follows: Select U1. * FROM M_user u1,m_user U2 where U1. Name = U2. Name and U1.rowid <> U2.rowid
found that when the user name of the same record more than two times there will be a problem, each record will be queried two times. For example,the name of the ID 1, 2, 3 Records is User1, then 1 and 3, 2 and 3, 1 and 2 will be queried,
That is, each record was detected two times. This is not possible, so back to the SQL statement. You can do this by using GROUP by. Copy Code Select * from m_user where namein (select Name ss from M_user where IsDeleted=0 GROUP by Name have count (*) > 1 )

Querying all records with the same name in the database

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.