SQL SERVER 表与表之间 字段一对多sql语句写法


表A

A1A2A3A4
01020304
03040102

表B

B1B2
01张三
02李四
03王五
04赵六

显示结果

A1A2A3A4
张三李四王五赵六
王五赵六张三李四

效果图:

sql语句如下

SELECT
(SELECT B2 FROM B WHERE B1=A1) AS A1,
(SELECT B2 FROM B WHERE B1=A2) AS A2,
(SELECT B2 FROM B WHERE B1=A3) AS A3,
(SELECT B2 FROM B WHERE B1=A4) AS A4
FROM A

这样就ok了