乡亲
我有 2 个表国家代码,cctonumbers,如下所述。
我有我尝试过的查询,但没有得到所需的输出。
我的输出
country destination country_code destination_code
Afghanistan Afghanistan Mobile Etisalat 93 78
Afghanistan Afghanistan Mobile Etisalat 93 72
Afghanistan Afghanistan Mobile Roshan 93 79
Afghanistan Afghanistan 93 93
期望的输出
country destination country_code destination_code
Afghanistan Afghanistan Mobile Etisalat 93 78
Afghanistan Afghanistan Mobile Etisalat 93 72
Afghanistan Afghanistan Mobile Roshan 93 79
使用的表格如下
国家代码表
id parentid countryname
1031 0 afghanistan
1035 1031 Afghanistan Mobile Etisalat
1036 1031 Afghanistan Mobile Roshan
cctonumbers 表
id countrycode_id parentid number
15731 1031 0 93
15197 1035 15731 78
15198 1035 15731 72
15199 1036 15731 79
我使用的查询如下,但没有得到想要的结果。
select *
from
cctonumbers
LEFT JOIN countrycodes as CC
ON cctonumbers.countrycode_id = CC.id
WHERE
( CC.parentid=0
AND number like '93%'
and cctonumbers.id in
( select cctonumbers.parentid
from cctonumbers
LEFT JOIN countrycodes as CC
ON cctonumbers.countrycode_id = CC.id
WHERE number like '7%'
AND CC.parentid!=0 )
)
or ( CC.parentid != 0
AND number like '7%'
AND CC.parentid in
( select CC.id
from cctonumbers
LEFT JOIN countrycodes as CC
ON cctonumbers.countrycode_id=CC.id
WHERE CC.parentid=0
AND number like '93%' )
)
ORDER BY
cctonumbers.number Asc