简体   繁体   English

将记录与来自两个不同表的通配符匹配

[英]Matching records with wild cards from two different tables

I have two tables with the following data (amongst other data). 我有两个表,其中包含以下数据(以及其他数据)。

Table 1 表格1

Value 1
'003232339639
'00264644106272
0026461226291#

I need to match the second column in the table below using column 1 as an identifier 我需要使用第1列作为标识符来匹配下表中的第二列

Table 2 表2

Value 1      Value 2
00264        1
0026485      2
0026481      3
00322889     4
00323283     5
00323288     6

So the results I need will be as follows: 因此,我需要的结果如下:

Result 结果

Table 1, Value 1   Table 2, Value 2         
'003232339639......4
'00264644106272....1
0026461226291#.....1

Any help will be appreciated - very stuck here and doing it manually at the moment in excel. 任何帮助将不胜感激-非常卡在这里,目前在excel中手动进行。

I hope this format makes sense - first time I am using this forum. 我希望这种格式有意义-第一次使用此论坛。

Melany, the question is kind of confusing (not written correctly) perhaps that's why no one is responding. 梅拉尼,这个问题有点让人困惑(写得不正确),也许这就是为什么没人回应的原因。 I'll make an attempt to explain how similar selects is done 我将尝试解释类似的选择是如何完成的

SELECTING DATA FROM TABLE1 WHERE A MATCHING COLUMN (COL1) EXISTS IN BOTH TABLE 两个表中都存在匹配列(COL1)的情况下从表1中选择数据

SELECT * FROM TABLE1
    INNER JOIN TABLE2
      ON TABLE1.COL1 = TABLE2.COL1
        AND TABLE1.COL1 = 'XYZ'

USING A SUBSELECT FOR THE SAME 使用相同的子选择

    SELECT * FROM TABLE1
            WHERE COL1 IN(SELECT COL1 FROM TABLE2
                           WHERE COL1 = 'XYZ')

In SQL, the wildcard for one or more characters is % , and is to be used with the keyword LIKE . 在SQL中,一个或多个字符的通配符为% ,并与关键字LIKE一起使用。

So I suggest the following (if your purpose is really to match rows in Table1 for which Value1 begins like a value in Table2.Value1 ): 因此,我建议采取以下措施(如果您的目的是真正匹配Table1中以Value1开始的行,如Table2.Value1的值):

SELECT Table1.Value1, Table2.Value2 WHERE Table1.Value1 LIKE CONCAT(Table2.Value1, '%');

Edit: replace CONCAT(x, y) with x || y 编辑:用x || y替换CONCAT(x, y) x || y for some DBMSs (SQLite for instance). 对于某些DBMS(例如SQLite)为x || y

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

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