group_concat()在多个表的多个列上

时间:2018-01-19 15:22:53

标签: sqlite

我正在尝试从数据库中查询2个表,并希望使用group_concat()只有一行结果。 但是通过我创建的查询,我得到的数据库不只是一次,而是多次,我不知道为什么:

我的查询:Link to db-fiddle.com

SELECT a.owner,
       group_concat(causConc, '') AS attributes,
       group_concat(causConc2, '') AS reqs
FROM (SELECT a.owner,
             '{' || key || ',' || group_concat(value, ',') || ';' || '}' AS causConc
      FROM attributes a
      WHERE a.owner = 162012
      GROUP BY a.owner,
               key) a
INNER JOIN (SELECT owner,
             '{' || key || ',' || group_concat(value, ',') || ';' || '}' AS causConc2
      FROM reqs r
      WHERE r.owner = 162012
      GROUP BY r.owner,
               key) r 
ON a.owner = r.owner
GROUP BY a.owner

任何人都可以告诉我为什么我多次获得这些数据库的命令?

提前致谢! :)

1 个答案:

答案 0 :(得分:0)

连接计算来自两个源的匹配行的所有可能组合。

要仅连接两个来源的行,请使用compound query

SELECT owner,
       group_concat(causConc, '') AS attributes,
       group_concat(causConc2, '') AS reqs
FROM (SELECT owner, causConc, NULL AS causConc2
      FROM (... first subquery ...)
      UNION ALL
      SELECT owner, NULL, causConc2
      FROM (... second subquery ...))
GROUP BY owner;