A表有字段a,B表有字段b,字段a中包含字段b,如何关联查询

比如 AA表如
xh  shengccj
1   河南羚锐生物药业                                  
2   四川蜀中制药有限公司                              
3   海口奇力制药

BB表 
xh  shengccj
1   河南羚锐生物
1   河南中杰药业                                     
2   四川蜀中制药
2   海口奇力制药
3   河南中杰药业 
我想查询 的条件为 A表xh=b表xh   a表中shengccj 必须在b表中shengccj的
结果为
xh shengccj
1   河南羚锐生物药业                                  
2   四川蜀中制药有限公司   

CREATE TABLE AA 
(
    xh INT,
    shengccj VARCHAR(100)
)
 
 
INSERT INTO AA
SELECT 1, '河南羚锐生物药业'
UNION ALL 
SELECT 2, '四川蜀中制药有限公司'
UNION ALL 
SELECT 3, '海口奇力制药'
 
CREATE TABLE BB 
(
    xh INT,
    shengccj VARCHAR(100)
)
INSERT INTO BB
SELECT 1, '河南羚锐生物'
UNION ALL 
SELECT 1, '河南中杰药业'
UNION ALL 
SELECT 2, '四川蜀中制药'
UNION ALL 
SELECT 2, '海口奇力制药'
UNION ALL 
SELECT 3, '河南中杰药业'
GO
--开始查询
--1
select a.xh,a.shengccj from AA a left join BB b  on  a.xh=b.xh  where   b.shengccj like substring(a.shengccj,1,6)
--2
select a.xh,a.shengccj from  AA a ,BB b where a.xh=b.xh and b.shengccj like left(a.shengccj,6)
--3
select a.xh,a.shengccj from AA a,BB b where a.xh=b.xh and a.shengccj like '%'+b.shengccj+'%' --这个也可以出结果啊
/*
-----------------------------------------------------------------------
xh    shengccj
1     河南羚锐生物药业   
2     四川蜀中制药有限公司
 
 
 
(2 行受影响)
------------------------------------------------------------------------
*/
原文地址:https://www.cnblogs.com/albert-/p/14690693.html