如何使用通配符加入两个表列?

时间:2012-08-29 16:30:37

标签: sql ms-access jet

我正在使用Access 97。

这就是我想做的事情:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber LIKE ListOfProducts.IDNumber*;

有时,ListOfProducts上的IDNumber会有一个额外的字母,表示有关该产品的一些信息。

所以我在CodeDIS上有IDNumber AC244,但我在ListOfProducts上有AC244P。

我想要的只是在条件结束时添加通配符。

这可能吗?

3 个答案:

答案 0 :(得分:1)

如果选择是C或P,为什么不:

SELECT *
FROM CodeDIS, ListOfProducts
WHERE CodeDIS.IDNumber = ListOfProducts.IDNumber & "C"
OR CodeDIS.IDNumber = ListOfProducts.IDNumber & "P"

答案 1 :(得分:1)

怎么样:

SELECT * 
FROM CodeDIS, ListOfProducts 
WHERE CodeDIS.IDNumber LIKE left(ListOfProducts.IDNumber,5); 

或者如果您愿意:

SELECT * 
FROM CodeDIS a inner join ListOfProducts b on a.IDNumber = left(b.IDNumber,5); 

答案 2 :(得分:1)

我知道这已经过时了,但是如果有的话,我没有发布太多内容,我想我会在这里试试。

如果最后一个数字始终是一个字符,为什么不使用带有PATINDEX测试的子字符串函数?我确信我的代码效率很低,但这是我的初学者尝试。我敢打赌,有些人可以将我的案件陈述变成更小的东西。

-- I used the code below
-- It only works if the final digit is lower or uppercase letter of 
-- specified language.
DECLARE @CodeDIS TABLE (IdNumber nvarchar(50), BaseRowNum nvarchar(50));

insert into @CodeDIS (IdNumber,BaseRowNum) values ('52352345','1')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('134131','2')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343141','3')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a143321','4')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('c34324','5')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1343214','6')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('%134324','7')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('a%134324','8')
insert into @CodeDIS (IdNumber,BaseRowNum) values ('1413','9')
;
DECLARE @ListOfProducts TABLE (IdNumber nvarchar(50), UpdateSourceDataColumn nvarchar(50));

insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('52352345a','11')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('134131','22')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343141a','33')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a143321','44')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('c343245','55')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1343214Z','66')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('%134324','77')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('a%134324','88')
insert into @ListOfProducts (IdNumber,UpdateSourceDataColumn) values ('1413\','99')
;
SELECT a.*,b.IdNumber as IdNumber2, b.UpdateSourceDataColumn
    FROM @CodeDIS a 
    left join @ListOfProducts b
    on a.IdNumber = 
      case 
        when PATINDEX(
          '[a-zA-Z]', 
          substring(b.IdNumber,len(b.IdNumber),1) 
          COLLATE Latin1_General_CS_AS
        )=1
        then left(b.IdNumber,len(b.IdNumber)-1)
        else b.IdNumber
      end
    ;     
--IdNumber  BaseRowNum  IdNumber2   UpdateSourceDataColumn
--52352345  1           52352345a   11
--134131    2           134131      22
--1343141   3           1343141a    33
--a143321   4           a143321     44
--c34324    5           NULL        NULL
--1343214   6           1343214Z    66
--%134324   7           %134324     77
--a%134324  8           a%134324    88
--1413      9           NULL        NULL