简体   繁体   English

mysql根据不同的列值重复搜索

[英]mysql duplicates search based on different column values

I am having two tables. 我有两张桌子。 1st table have id, First name, last name and phone number columns. 第一个表具有ID,名字,姓氏和电话号码列。 2nd table have state, district, block, and village columns. 第二个表具有州,地区,街区和村庄列。 id column have unique values. id列具有唯一值。 Other columns can have duplicate values. 其他列可以具有重复值。

I want to select all records having same first name, last name, state, district, block and village values but different phone number values. 我想选择所有具有相同名字,姓氏,州,地区,街区和村庄值但电话号码值不同的记录。

for example: 例如:

ID  First Name  Phone Number    District    State   Block   Village
1   Rajesh      9876543210       ABC        XYZ     GHI     PQR
2   Jim         7894561230       WXY        DEF     JKL     SDF
3   Jack        8745963210       EWQ        REW     YTR     POI
4   Rajesh      9856741230       ABC        XYZ     GHI     PQR
5   Jack        8745963210       EWQ        REW     YTR     POI

The out put should include 1st and 4th records and not 3rd and 5th as they are having same phone number. 输出应包含第一和第四记录,而不是第三和第五记录,因为它们具有相同的电话号码。

I have selected all duplicate rows with same phone number by using the following query: 我通过使用以下查询选择了具有相同电话号码的所有重复行:

SELECT b.phone_mobile, g.countID, b.id
FROM (
SELECT phone_mobile, COUNT( id ) AS countID
FROM contacts
GROUP BY phone_mobile                
HAVING COUNT( id ) >1
)g
INNER JOIN contacts b ON b.phone_mobile = g.phone_mobile

Now I am not getting how to select all rows with same name and location but different phone number. 现在,我不知道如何选择名称和位置相同但电话号码不同的所有行。 I need help. 我需要帮助。

Thank you. 谢谢。

You should select all rows if there is a row with the same name,address but another ID and Phone_number: 如果存在具有相同名称,地址但另一个ID和Phone_number的行,则应选择所有行:

SELECT *
FROM contacts c
JOIN address a on c.id=a.id
WHERE EXISTS
 (
  SELECT 1
    FROM contacts c1
    JOIN address a1 on c1.id=a1.id
    WHERE c1.ID<>c.id
          AND c1.Phone_Number<>c.Phone_Number
          AND c1.First_Name=c.First_Name
          AND a1.District=a.District
          AND a1.State=a.State
          AND a1.Block=a.Block
          AND a1.Village=a.Village
  )

SQLFiddle demo SQLFiddle演示

I have a suggestion for you. 我有你的建议。 At times its good to have a correct database schema. 有时拥有正确的数据库架构是件好事。

What I mean by correct is. 我的意思是正确的。 If you use normalization here while making database. 如果在创建数据库时在此处使用规范化。 You will have to deal with much simpler query. 您将不得不处理更简单的查询。

After observing your tables. 观察你的桌子之后。 I would recommend having Phone column in a different table. 我建议在其他表格中使用“电话”列。 So you could have like. 所以你可能会喜欢。 Each user can have many phone numbers, doing so will solve the problem of querying. 每个用户可以拥有许多电话号码,这样做将解决查询问题。

So now when you have taken phone column out of the table. 因此,现在您将电话专栏从表中取出时。 Automatically you will have distinct user rows with multiple phone number. 自动地,您将拥有带有多个电话号码的不同用户行。 And whenever you pull a user just have a where clause to connect to the phone number table and there you go. 每当您拉用户时,只要有一个where子句即可连接到电话号码表,然后就可以了。 You have user with multiple unique phone numbers. 您的用户具有多个唯一的电话号码。

Select * from users U,phone P where U.id = P.user_id

Or 要么

Select * from users U, phone P where U.id = P.user_id and U.firstname = 'john'

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM