简体   繁体   English

匹配来自两个不同表的两列的字符串

[英]Matching Strings from two columns from two different tables

I have created two tables, lets call them table a and table b.我创建了两个表,我们称它们为表 a 和表 b。

Table A consists of one column of strings called domainType表 A 由一列名为 domainType 的字符串组成

Table B consists of one column of strings which are substrings to table A's column (called topLevelDomain)表 B 由一列字符串组成,这些字符串是表 A 列的子字符串(称为 topLevelDomain)

CREATE TABLE a ( rank int, name text, domainType text )

CREATE TABLE b ( topLevelDomain text, domainDescription text)

table a:表一:

 rank     name      domainType
 1        a          com
 2        b          co jp
 3        c          co cn

table b:表b:

topLevelDomain    domainDescription
com               country1
in                country2
cn                country3
...
jp                country30

I want to list the most popular descriptions(countries) according to the rank of the domainType to show this:我想根据 domainType 的排名列出最受欢迎的描述(国家/地区)以显示这一点:

Desired result:想要的结果:

country1 -------> since rank is 1 (in table a)
country30 ------> since rank is 2 (in table a)
country2 --------> since rank is 3 (in table a)

I'm having trouble 'relating' the topLevelDomain column with the domainType column and returning the descriptions with the top rank corresponding to the domainType.我在将 topLevelDomain 列与 domainType 列“关联”并返回具有与 domainType 对应的最高等级的描述时遇到问题。 I hope this makes sense!我希望这是有道理的! please let me know to add anymore information.请让我知道添加更多信息。

You can join the tables with the use of the operator LIKE :您可以使用运算符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

See the demo .请参阅演示
Results:结果:

| domaindescription |
| ----------------- |
| country1          |
| country30         |
| country3          |
| country2          |

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

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