如何使用一列

时间:2018-05-17 14:32:13

标签: sql

我有2个表表A有一个服务器列表,表B是目录中的文件名转储。 表A的例子

ID Server
1 Development.Stage
2 Development.DEV
3 Development.QA
4 Production
5 APPS

等...

表B的例子

FileName
Development.Stage.V01B01.ConferenceApp.Bruce.20170106
Development.Stage.V01B02.ConferenceApp.Michael.20170612
Development.DEV.V01B01.AdvantageUpgrade.Bruce.20171121
Development.DEV.V01B02.AdvantageUpgrade.Bruce.20180102
Development.QA.V02B01.AdvantageUpgrade.Bruce.20180306
Development.QA.V02B02.AdvantageUpgrade.Bruce.20180415
Development.QA.V02B03.AdvantageUpgrade.Bruce.20180511
Production.V03B01.AdvantageUpgrade.Bruce.20180410
Production.V03B02.AdvantageUpgrade.Bruce.20180503
Development.Stage.V01B01.AnnualMeeting.Bruce.20160724
Development.Stage.V01B03.AnnualMeeting.Shane.20170719

等...

我希望能够使用表B中与表A中的Server列匹配的子字符串将表A连接到表B.

4 个答案:

答案 0 :(得分:0)

尝试这样的事情:

select * from tableA a join tableB b on b.filename like '%'+a.server+'%' 

答案 1 :(得分:0)

    Select  *
    FROM TableA as a
    JOIN TableB as b 
on b.FileName like a.Server+'%' 

答案 2 :(得分:0)

ANSI标准解决方案如下:

select . . .
from a join
     b
     on b.filename = a.server || '%';

请注意,在大多数数据库中,这可能效率低下。

编辑:

SQL Server使用+代替||

select . . .
from a join
     b
     on b.filename = a.server + '%';

答案 3 :(得分:0)

SELECT a.id, a.server, b.filename
FROM a 
LEFT JOIN b ON (a.server = SUBSTRING_INDEX(b.filename, '.', 2));

这应该适用于MySQL DBMS。