如何连接两个表并动态进行数据透视

时间:2018-11-09 16:52:28

标签: mysql sql

我有两个表A和B

A(id,name)
B(a_id,键,值)

带有一些值的表

**A table**

-----------------------------
   id        |      name
-----------------------------
    1        |      sorabh
    2        |      john
-----------------------------

**B table**

-------------------------------------------------
     a_id    |     key     |     value
-------------------------------------------------
      1      |    looks    |    handsome
      1      |    lazy     |    yes
      1      |    car      |    honda
      2      |    phone    |    948373221
      1      |    email    |    some@ccid.com
-------------------------------------------------

现在我要实现的是一个跟踪,具有单个查询,内部联接,交叉联接等。

SELECT * FROM A
CROSS JOIN B WHERE A.id=1

结果必须类似于

--------------------------------------------------------------------
  id   |   name   |    looks   |   lazy  |    car   |   email
--------------------------------------------------------------------
   1   |  sorabh  |   handsome |   yes   |  honda   | some@ccid.com 
--------------------------------------------------------------------

5 个答案:

答案 0 :(得分:5)

假设id的{​​{1}}列是table a列,而primary key具有table ba_id列的复合唯一键,组合或没有重复项,即使没有这样的限制,相关的子查询也可以用作:

key

答案 1 :(得分:3)

您可以尝试使用mysql动态数据透视表来达到期望。

使用条件汇总功能进行枢轴操作

准备好您的SQL语句,并用于动态执行SQL EXECUTE stmt;

SET @sql = NULL;
SELECT
  GROUP_CONCAT(DISTINCT
    CONCAT(
      'max(case when `key`= ''',
      `key`,
      ''' then `value` end) AS ',
      `key`
    )
  ) INTO @sql
FROM A join B on a.id=b.a_id
WHERE b.a_id = 1;

SET @sql = CONCAT('select a.id,a.name, ', @sql, ' 
                   FROM A join B on a.id=b.a_id
                    WHERE b.a_id = 1
                    group by a.name');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

sqlfiddle

结果

id  name    looks       laz   car   email
1   sorabh  handsome    yes   honda some@ccid.com

引用

Dynamic pivot tables

答案 2 :(得分:2)

for f in glob.glob("C:\Consoildated_DailyReports\Hold*.xlsx"): df = pd.read_excel(f,sheet_name='Data', dtype={"col_name": object}) df = df.astype(str) #df.to_html() data1 = data1.append(df,ignore_index=True)

一起使用的情况
max()

答案 3 :(得分:2)

尝试一下

WITH CTE_A AS (
   SELECT
      A.id,
      A.name,
      B.key,
      B.value
   FROM  A
    INNER JOIN B ON A.id = B.a_id
)
SELECT *
FROM
   CTE_A
   PIVOT (max(value) FOR key IN (looks, lazy, car,phone,email)) P
;

如果键和值是动态的,请使用以下

DECLARE @colsToPivot AS NVARCHAR(MAX),
        @sqlStmt  AS NVARCHAR(MAX)
select @colsToPivot = STUFF((SELECT distinct ',' + QUOTENAME(key) 
                    from B
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')


set @sqlStmt = 'WITH CTE_A AS (
       SELECT
          A.id,
          A.name,
          B.key,
          B.value
       FROM  A
        INNER JOIN B ON A.id = B.a_id
    )
    SELECT *
    FROM
       CTE_A
       PIVOT (max(value) FOR key IN (' + @colsToPivot +')) P'

execute(@sqlStmt)

答案 4 :(得分:-3)

SELECT * FROM B LEFT JOIN A WHERE A.id=B.a_id AND A.id=1