如何在MySQL中没有重复的情况下退出联接

时间:2020-09-12 22:46:21

标签: mysql join

我正在编写一个包含10个以上联接的查询。

目前,我只剩下一个LEFT JOIN零件。

我需要为“法律实体”加入“要约”,然后为法人实体加入“要件”,然后对要件进行越来越多的选择,但是我被困在重复的要件上-一个法人有两个要件,和3个报价。

表格“提供”:

+----------+-----------------+
| OFFER_ID | LEGAL_ENTITY_ID |
+----------+-----------------+
|        1 |               3 |
|        2 |               3 |
|        3 |               3 |
+----------+-----------------+

表“ legal_entites”:

+-----------------+
| LEGAL_ENTITY_ID |
+-----------------+
|               3 |
+-----------------+

表“必备条件”:

+--------------+-----------------+
| REQUISITE_ID | LEGAL_ENTITY_ID |
+--------------+-----------------+
|            3 |               3 |
|            4 |               3 |
+--------------+-----------------+

表格“余额”:

+------------+----------+---------------+
| BALANCE_ID | OFFER_ID | OFFER_BALANCE |
+------------+----------+---------------+
|          1 |        1 |           100 |
|          2 |        2 |           400 |
|          3 |        3 |           600 |
+------------+----------+---------------+

查询:

SELECT  
    LE.LEGAL_ENTITY_ID,
    SUM(BAL.OFFER_BALANCE) SUM_OF_BALANCES,
    GROUP_CONCAT(REQ.REQUISITE_ID) REQS
FROM
    `legal_entities` LE

LEFT JOIN 
    offers OFR ON OFR.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID

LEFT JOIN 
    `requisites` REQ ON REQ.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
    
LEFT JOIN
    `balances` BAL ON BAL.OFFER_ID = OFR.OFFER_ID

WHERE
    LE.LEGAL_ENTITY_ID = 3
    
GROUP BY
    LE.LEGAL_ENTITY_ID;

输出:

+-----------------+-----------------+-------------+
| LEGAL_ENTITY_ID | SUM_OF_BALANCES | REQS        |
+-----------------+-----------------+-------------+
|               3 |            2200 | 3,4,3,4,3,4 |
+-----------------+-----------------+-------------+

预期输出:

+-----------------+-----------------+-------------+
| LEGAL_ENTITY_ID | SUM_OF_BALANCES | REQS        |
+-----------------+-----------------+-------------+
|               3 |            1100 | 3,4         |
+-----------------+-----------------+-------------+

如何实现预期的输出?当然,我可以GROUP_CONCAT(DISTINCT ...),但是实际数据将保持不变,并且如果我将另一个表加入“合法实体”,我将得到不正确的总和。

更新:添加了“余额”联接以显示重复项的全部问题,因为人们不了解真正的问题。

更新2:solutionnbk可以正常工作:

SELECT
    LE.LEGAL_ENTITY_ID,
    SUM( BAL.OFFER_BALANCE ) SUM_OF_BALANCES,
    MIN(REQ.REQS) 
FROM
    `legal_entities` LE
    
LEFT JOIN 
    offers OFR ON OFR.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
    
LEFT JOIN 
    `balances` BAL ON BAL.OFFER_ID = OFR.OFFER_ID
    
LEFT JOIN 
    (SELECT GROUP_CONCAT(DISTINCT REQUISITE_ID) REQS, R.* FROM `requisites` R GROUP BY LEGAL_ENTITY_ID) REQ ON REQ.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID 
    
GROUP BY
    LE.LEGAL_ENTITY_ID

2 个答案:

答案 0 :(得分:0)

就像您说的那样,将DISTINCT添加到GROUP_CONCAT将消除重复项。 不过,您有重复项的原因是因为您要加入offers表,因此,如果删除该联接,它将删除重复项。

SELECT 
    LE.LEGAL_ENTITY_ID,
    GROUP_CONCAT(REQ.REQUISITE_ID) REQS
FROM `legal_entities` AS LE
LEFT JOIN `requisites` AS REQ 
    ON REQ.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
WHERE
    LE.LEGAL_ENTITY_ID = 3
GROUP BY
    LE.LEGAL_ENTITY_ID;

这是fiddle,显示了它的作用

答案 1 :(得分:0)

只需在GROUP_CONCAT中使用DISTINCT。

您可以跳过GROUP BY

CREATE TABLE `offers`(
  OFFER_ID INT, LEGAL_ENTITY_ID INT);
  
  INSERT INTO `offers`(OFFER_ID, LEGAL_ENTITY_ID)
  VALUES (1,3),(2,3),(3,3);
  
  CREATE TABLE LEGAL_ENTITIES(
    LEGAL_ENTITY_ID INT);
   
   INSERT INTO LEGAL_ENTITIES(LEGAL_ENTITY_ID)
   VALUES (3);
   
   CREATE TABLE `requisites`(
     REQUISITE_ID INT, LEGAL_ENTITY_ID INT);
   
   INSERT INTO `requisites`(REQUISITE_ID, LEGAL_ENTITY_ID)
   VALUES (3, 3), (4,3);
   
   CREATE TABLE balances (
  `BALANCE_ID` INTEGER,
  `OFFER_ID` INTEGER,
  `OFFER_BALANCE` INTEGER
);

INSERT INTO balances
  (`BALANCE_ID`, `OFFER_ID`, `OFFER_BALANCE`)
VALUES
  ('1', '1', '100'),
  ('2', '2', '400'),
  ('3', '3', '600');
SELECT  
    LE.LEGAL_ENTITY_ID,
    SUM(BAL.OFFER_BALANCE) SUM_OF_BALANCES,
    GROUP_CONCAT(DISTINCT REQ.REQUISITE_ID) REQS
FROM
    `LEGAL_ENTITIES` LE

LEFT JOIN 
    offers OFR ON OFR.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID

LEFT JOIN 
    `requisites` REQ ON REQ.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
    
LEFT JOIN
    `balances` BAL ON BAL.OFFER_ID = OFR.OFFER_ID

WHERE
    LE.LEGAL_ENTITY_ID = 3
    
LEGAL_ENTITY_ID | SUM_OF_BALANCES | REQS
--------------: | --------------: | :---
              3 |            2200 | 3,4 
SELECT
LE.LEGAL_ENTITY_ID, SUM(BAL.OFFER_BALANCE) SUM_OF_BALANCES, MIN(REQ.REQS)
FROM  `LEGAL_ENTITIES` LE
LEFT JOIN
offers OFR ON OFR.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
LEFT JOIN
`balances` BAL ON BAL.OFFER_ID = OFR.OFFER_ID
LEFT JOIN 
(SELECT GROUP_CONCAT(DISTINCT REQUISITE_ID) REQS, LEGAL_ENTITY_ID FROM `requisites` GROUP BY LEGAL_ENTITY_ID) 
 REQ ON REQ.LEGAL_ENTITY_ID = LE.LEGAL_ENTITY_ID
 GROUP BY LE.LEGAL_ENTITY_ID
 
LEGAL_ENTITY_ID | SUM_OF_BALANCES | MIN(REQ.REQS)
--------------: | --------------: | :------------
              3 |            1100 | 3,4          

db <>提琴here