簡體   English   中英

多個表上的mysql group_concat

[英]mysql group_concat on multiple tables

這是我第一次使用stackoverflow,...對我溫柔;)

在地圖表上使用多個JOIN時,我從GROUP_CONCAT獲取重復結果時遇到的問題很少。

解釋這個並不容易,但我會嘗試:

我創建了一個用於測試的SQLFiddle: http ://sqlfiddle.com/#!9 / d2b347 / 3

對於所有帖子,我希望查詢只是一個而不是1,然后對每個測試進行錘擊。 但是由於GROUP_CONCAT正在合並這些測試結果,我得到的數據是我想要的兩倍。

有可能以某種方式使查詢更可靠。 要始終將GROUP_CONCAT作為確切的測試次數?

我希望/希望輸出為:

|---------|-----------------|------------|---------|-------------|
| post_id | flows           | flow_types | powers  | power_types |
|---------|-----------------|------------|---------|-------------|
|       1 | 100,140         | a,b        | 1,1     | a,b         |
|---------|-----------------|------------|---------|-------------|
|       2 | 200,200,200     | a,b,c      | (null)  | (null)      |
|---------|-----------------|------------|---------|-------------|

但它是:

|---------|-----------------|------------|---------|-------------|
| post_id | flows           | flow_types | powers  | power_types |
|---------|-----------------|------------|---------|-------------|
|       1 | 100,100,140,140 | a,a,b,b    | 1,1,1,1 | a,b,a,b     |
|---------|-----------------|------------|---------|-------------|
|       2 | 200,200,200     | a,b,c      | (null)  | (null)      |
|---------|-----------------|------------|---------|-------------|

和GROUP_CONCAT DISTINCT我得到:

|---------|-----------------|------------|---------|-------------|
| post_id | flows           | flow_types | powers  | power_types |
|---------|-----------------|------------|---------|-------------|
|       1 | 100,140         | a,b        | 1       | a,b         |
|---------|-----------------|------------|---------|-------------|
|       2 | 200             | a,b,c      | (null)  | (null)      |
|---------|-----------------|------------|---------|-------------|

這是創建查詢:

DROP TABLE IF EXISTS `posts`;
CREATE TABLE IF NOT EXISTS `posts` (
  `post_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `post` varchar(256) CHARACTER SET ascii NOT NULL,
  PRIMARY KEY (`post_id`),
  UNIQUE KEY `UNQ_post` (`post`) USING HASH
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `posts_test1`;
CREATE TABLE IF NOT EXISTS `posts_test1` (
  `post_id` bigint(20) unsigned NOT NULL,
  `test1_id` bigint(20) unsigned NOT NULL,
  `type_id` int(10) unsigned NOT NULL DEFAULT 1,
  PRIMARY KEY (`post_id`,`test1_id`,`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `test1`;
CREATE TABLE IF NOT EXISTS `test1` (
  `test1_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `flow` int(10) unsigned NOT NULL,
  PRIMARY KEY (`test1_id`),
  KEY `IDX_FLOW` (`flow`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `posts_test2`;
CREATE TABLE IF NOT EXISTS `posts_test2` (
  `post_id` bigint(20) unsigned NOT NULL,
  `test2_id` bigint(20) unsigned NOT NULL,
  `type_id` int(10) unsigned NOT NULL DEFAULT 1,
  PRIMARY KEY (`post_id`,`test2_id`,`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `test2`;
CREATE TABLE IF NOT EXISTS `test2` (
  `test2_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `power` int(10) unsigned NOT NULL,
  PRIMARY KEY (`test2_id`),
  KEY `IDX_POWER` (`power`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `types`;
CREATE TABLE IF NOT EXISTS `types` (
  `type_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(50) CHARACTER SET ascii DEFAULT NULL,
  PRIMARY KEY (`type_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;


INSERT INTO `types` (`type_id`, `type`) VALUES
  (1, 'a'),
  (2, 'b'),
  (3, 'c');

INSERT INTO `posts` (`post_id`, `post`) VALUES
  (1, 'test1'),
  (2, 'test2');

INSERT INTO `test1` (`test1_id`, `flow`) VALUES
  (1, 100),
  (2, 140),
  (3, 200),
  (4, 200),
  (5, 200);

INSERT INTO `posts_test1` (`post_id`, `test1_id`, `type_id`) VALUES
  (1, 1, 1),
  (1, 2, 2),
  (2, 3, 1),
  (2, 4, 2),
  (2, 5, 3);

INSERT INTO `test2` (`test2_id`, `power`) VALUES
  (1, 1),
  (2, 1);

INSERT INTO `posts_test2` (`post_id`, `test2_id`, `type_id`) VALUES
  (1, 1, 1),
  (1, 2, 2);

這是我的選擇查詢..

SELECT
p.post_id, p.post,
GROUP_CONCAT(t1.flow) flow,
GROUP_CONCAT(t1t.type) flow_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
    LEFT JOIN test1 t1 USING (test1_id)
        LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
GROUP BY p.post_id; # works fine


SELECT
p.post_id, p.post,
GROUP_CONCAT(t2.power) powers,
GROUP_CONCAT(t2t.type) power_types
FROM posts p
LEFT JOIN posts_test2 pt2 USING (post_id)
    LEFT JOIN test2 t2 USING (test2_id)
        LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # works fine


SELECT
p.post_id, p.post,
GROUP_CONCAT(t1.flow) flow,
GROUP_CONCAT(t1t.type) flow_types,
GROUP_CONCAT(t2.power) powers,
GROUP_CONCAT(t2t.type) power_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
    LEFT JOIN test1 t1 USING (test1_id)
        LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
LEFT JOIN posts_test2 pt2 USING (post_id)
    LEFT JOIN test2 t2 USING (test2_id)
        LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # getting duplicated GROUP_CONCAT results

SELECT
p.post_id, p.post,
GROUP_CONCAT(DISTINCT t1.flow) flow,
GROUP_CONCAT(DISTINCT t1t.type) flow_types,
GROUP_CONCAT(DISTINCT t2.power) powers,
GROUP_CONCAT(DISTINCT t2t.type) power_types
FROM posts p
LEFT JOIN posts_test1 pt1 USING (post_id)
    LEFT JOIN test1 t1 USING (test1_id)
        LEFT JOIN types t1t ON (t1t.type_id = pt1.type_id)
LEFT JOIN posts_test2 pt2 USING (post_id)
    LEFT JOIN test2 t2 USING (test2_id)
        LEFT JOIN types t2t ON (t2t.type_id = pt2.type_id)
GROUP BY p.post_id; # DISTINCT wipes the GROUP_CONCAT if same result...

感謝,並有一個愉快的一天!!

編輯:添加預期結果作為建議,謝謝:)

這里的問題是有兩個不同的連接表(和兩個不同的連接鏈),源自一個表格post 所以線性JOIN鏈不起作用。 當完成線性連接時,其中一個連接表中的重復導致其他鏈中的重復。

一種方法是在兩個單獨的派生表FROM子句中的子查詢)中考慮這兩個不同的JOIN鏈,並確定它們各自的分組/聚合表達式。 然后我們可以使用post_id JOIN這兩個鏈。

詢問

SELECT
  dt1.post_id, 
  dt1.flows, 
  dt1.flow_types, 
  dt2.powers, 
  dt2.power_types 
FROM 
(
  SELECT 
    p.post_id, 
    GROUP_CONCAT(t1.flow) AS flows, 
    GROUP_CONCAT(typ.type) AS flow_types
  FROM posts p
  LEFT JOIN posts_test1 pt1 
    ON pt1.post_id = p.post_id 
  LEFT JOIN test1 t1 
    ON t1.test1_id = pt1.test1_id 
  LEFT JOIN types typ 
    ON typ.type_id = pt1.type_id 
  GROUP BY p.post_id 
) AS dt1 
JOIN 
(
  SELECT 
    p.post_id, 
    GROUP_CONCAT(t2.power) AS powers, 
    GROUP_CONCAT(typ.type) AS power_types 
  FROM posts p
  LEFT JOIN posts_test2 pt2 
    ON pt2.post_id = p.post_id 
  LEFT JOIN test2 t2 
    ON t2.test2_id = pt2.test2_id 
  LEFT JOIN types typ 
    ON typ.type_id = pt2.type_id 
  GROUP BY p.post_id 
) AS dt2
  ON dt1.post_id = dt2.post_id;

結果

| post_id | flows       | flow_types | powers | power_types |
| ------- | ----------- | ---------- | ------ | ----------- |
| 1       | 100,140     | a,b        | 1,1    | a,b         |
| 2       | 200,200,200 | a,b,c      |        |             |

查看DB小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM