Select without duplicate from a specific string/key

会有一股神秘感。 提交于 2019-12-11 16:48:50

问题


Thanks to @Ed Gibbs i managed to solve my first problem on this case (Select duplicate and keep the oldest (not based on ID))

I am now facing a new problem I can not solve.

I have two tables, "domain" which is clear of duplicate and "email" which contains duplicate. In the first table i had a value called "creationdate" which i used as a filter. In the second table i don't have any filter but some informations could (i think) be used to act as a filter.

Table domain :

| domain     | value 1 | foreign_key |
|------------|---------|-------------|
| google.com | patrick | X           |
| yahoo.com  | britney | Y           |
| ebay.com   | harry   | Z           |

Table email :

| email              | value 1 | foreign_key |
|--------------------|---------|-------------|
| john@google.com    | patrick | X           |
| john@google.com    | britney | Y           |
| harry@google.com   | mary    | X           |
| mickael@google.com | jack    | X           |
| david@ebay.com     | walter  | Z           |
| alice@yahoo.com    | brian   | Y           |

As you can see on the first table, the domain google.com is handled by X foreign_key. In the email table the records "john@google.com,patrick,X" and "harry@google.com,mary,X" are fine because they match to the right foreign_key. The problem is records like "john@google.com,britney,Y", Y isn't the associated foreign_key to the domain google.com so i want to remove it.

Here is the desired table :

| email              | value 1 | foreign_key |
|--------------------|---------|-------------|
| john@google.com    | patrick | X           |
| harry@google.com   | mary    | X           |
| mickael@google.com | jack    | X           |
| david@ebay.com     | walter  | Z           |
| alice@yahoo.com    | brian   | Y           |

How can i select theses datas without the wrongs records ? I think the key of the problem is a concat/substring but i can't figure how to do it.

Thanks for your help.


回答1:


To get domain out of a proper email you can use substring_index() function and use a simple join based on foreign key and domain match.

SELECT email.* FROM email 
JOIN domain ON email.foreign_key = domain.foreign_key
               AND substring_index( email.email, '@', -1 ) = domain.domain


来源:https://stackoverflow.com/questions/16802139/select-without-duplicate-from-a-specific-string-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!