SQL 将行表与列表连接起来 [英] SQL Join a row table with a column table

查看:92
本文介绍了SQL 将行表与列表连接起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表要连接(连接).表1为列表如下:

I have two tables that I’m trying to connect (join). Table1 is a column table as follows:

<头>
id电话姓名说明
101123456玛丽亚ABC
102234567丹尼尔定义

表2为行表如下:

<头>
id属性价值
101经理鲁道夫
101帐户456
101代码B
102经理安娜
102代码B
102代码C

我正在寻找的结果是:

<头>
id电话姓名说明经理帐号代码
101123456玛丽亚ABC鲁道夫456B
102234567丹尼尔定义安娜B,C

推荐答案

您可以三次加入同一个表(使用不同的别名).例如:

You can join the same table thrice (using different aliases). For example:

select
  p.*,
  a.value as Manager,
  b.value as Account,
  c.value as Cardno
from table1 p
left join table2 a on a.id = p.id and a.attribute = 'Manager'
left join table2 b on b.id = p.id and b.attribute = 'Account'
left join table2 c on c.id = p.id and b.attribute = 'Cardno'

这篇关于SQL 将行表与列表连接起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆