我已经创建了两个表,我们称它们为表a和表b。
表A由一串称为domainType的字符串组成
表B由一列字符串组成,这些字符串是表A的列的子字符串(称为topLevelDomain)
CREATE TABLE a ( rank int, name text, domainType text )
CREATE TABLE b ( topLevelDomain text, domainDescription text)
表a:
rank name domainType
1 a com
2 b co jp
3 c co cn
表b:
topLevelDomain domainDescription
com country1
in country2
cn country3
...
jp country30
我想根据domainType的排名列出最受欢迎的描述(国家)显示此:
所需结果:
country1 -------> since rank is 1 (in table a)
country30 ------> since rank is 2 (in table a)
country2 --------> since rank is 3 (in table a)
我在将topLevelDomain列与domainType列“关联”并返回与domainType对应的最高排名的描述时遇到麻烦。我希望这是有道理的!请让我知道以添加更多信息。谢谢!
您可以使用运算符LIKE
联接表:
select b.domaindescription
from b left join a
on concat(' ', a.domaintype, ' ') like concat('% ', b.topleveldomain, ' %')
order by case when a.domaintype is null then 1 else 0 end, a.rank
请参见demo。结果:
| domaindescription |
| ----------------- |
| country1 |
| country30 |
| country3 |
| country2 |