sql—连接两个(a,b)表(连接a的键:1行=1个数据;b中的连接键:1行=多个数据)

zbdgwd5y  于 2021-06-28  发布在  Hive
关注(0)|答案(4)|浏览(394)

我有两张tablea和b。我怎样才能把这两张table连接起来?

id  |group
6ZE |A
UZC |A
LQY |B
HO7 |B
V4P |C
KR0 |C

b

id                 |DATA
6ZE, FET, UZX      |50048
UZC, LQY           |89871
LQY, E03           |31579
HO7, 7KT, LXW, EK8 |17260
V4P, MKP, TPJ      |37344
KR0                |50044
t9aqgxwy

t9aqgxwy1#

你可以试着跟着。。。

select * from A, B where B.id like concat('%', A.id ,'%');
stszievb

stszievb2#

试试这个

select *
from Table1 A
join Table2 B on A.id in split(B.id,'\\, ')
jucafojl

jucafojl3#

ddl和dml

CREATE TABLE A_TABLE
          (ID VARCHAR2(50),GROUPP VARCHAR2(50))

          INSERT INTO A_TABLE(ID,GROUPP)VALUES('6ZE','A');
          INSERT INTO A_TABLE(ID,GROUPP)VALUES('UZC','A');
          INSERT INTO A_TABLE(ID,GROUPP)VALUES('LQY','B');
          INSERT INTO A_TABLE(ID,GROUPP)VALUES('HO7','B');
          INSERT INTO A_TABLE(ID,GROUPP)VALUES('V4P','C');
          INSERT INTO A_TABLE(ID,GROUPP)VALUES('KR0','C');
          COMMIT;

          CREATE TABLE B_TABLE
          (ID VARCHAR2(50),DATA VARCHAR2(50));

          INSERT INTO B_TABLE(ID,DATA)VALUES('6ZE, FET, UZX','50048');
          INSERT INTO B_TABLE(ID,DATA)VALUES('UZC, LQY','89871');
          INSERT INTO B_TABLE(ID,DATA)VALUES('LQY, E03','31579');
          INSERT INTO B_TABLE(ID,DATA)VALUES('HO7, 7KT, LXW, EK8','17260');
          INSERT INTO B_TABLE(ID,DATA)VALUES('V4P, MKP, TPJ','37344');
          INSERT INTO B_TABLE(ID,DATA)VALUES('KR0','50044');
          COMMIT;

然后选择脚本

SELECT A.ID,A.GROUPP,B.ID,B.DATA
              FROM
              A_TABLE A
              JOIN
              B_TABLE B
              ON B.ID LIKE '%'||A.ID||'%'

拆分不是常见的函数

n3ipq98p

n3ipq98p4#

SELECT *
FROM A_TABLE INNER JOIN B_TABLE ON FIND_IN_SET(A_TABLE.id, B_TABLE.id)

相关问题