我有一个表Data,如下所示:
Name Address Country
A PO 123 AF Afghanistan
B PO 23 AFG AF
C PO IND df1 AFG
D PO 12345 USA我想要所有有Address和Country不同的记录。在上面的例子中,我需要记录3 (Name = C),因为Address有"IND“,Country是"AFG”。
注意:
Country可以是ISO-2,ISO-3或描述(Ex )."IN“、"IND”或"India")Address可能包含国家名称,也可能不包含国家名称-如果包含,可以是ISO-2,ISO-3或描述.Country详细信息存储在单独的国家表中,并将所有国家存储为-
Id _ Iso2 _ Iso3 _我肯定,
提前感谢您的帮助。
发布于 2017-09-21 23:34:28
你想在你的结果中使用美国队吗?
SQL Fiddle
MySQL 5.6架构设置
查询1
select *
from Data
inner join country
on (
Data.country = country.Iso2
or Data.country = country.Iso3
or Data.country = country.Description
)
Where
Data.Address NOT like concat('%',country.Iso2,'%')
and Data.Address NOT like concat('%',country.Iso3,'%')
and Data.Address NOT like concat('%',country.Description,'%')结果
| Name | Address | Country | Id | Iso2 | Iso3 | Description |
|------|------------|---------|----|------|------|--------------------------|
| C | PO IND df1 | AFG | 1 | AF | AFG | Afghanistan |
| D | PO 12345 | USA | 3 | US | USA | united states of america |查询2
select Data.*, country.*
from Data
inner join country
on (
Data.country = country.Iso2
or Data.country = country.Iso3
or Data.country = country.Description
)
left join country ca
on (
Data.Address like concat('%',ca.Iso2,'%')
or Data.Address like concat('%',ca.Iso3,'%')
or Data.Address like concat('%',ca.Description,'%')
)
Where
Data.Address NOT like concat('%',country.Iso2,'%')
and Data.Address NOT like concat('%',country.Iso3,'%')
and Data.Address NOT like concat('%',country.Description,'%')
and ca.id IS NOT NULL结果
| Name | Address | Country | Id | Iso2 | Iso3 | Description |
|------|------------|---------|----|------|------|-------------|
| C | PO IND df1 | AFG | 1 | AF | AFG | Afghanistan |https://stackoverflow.com/questions/46344565
复制相似问题