简体   繁体   中英

Multiple LEFT JOIN doesn't work on SUM()

So the idea is to get information from 3 tables:

  1. R_Regle: rule table, basic info of the rule
  2. C_Commentaire: the number of comments this rule has
  3. I_Interet: The SUM of all upvotes and downvotes from one rule.

The structure of my I_Interet: I_ID / I_Idregle / I_Idpseudo / I_Upvote / I_Downvote

Each row can only contain 1 upvote or 1 downvote.

so with this request:

    SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape, COUNT(C_Commentaire.C_IdRegle) AS CountComment, SUM(I_Interet.I_Up) AS Up ,SUM(I_Interet.I_Down) AS Down 
    FROM R_Regle
            LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
            LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id 
     GROUP BY R_ID

I try to obtain all those information.

Problem is:

COUNT(C_Commentaire.C_IdRegle) AS CountComment

Gives me the count of all COMMENTS and UPVOTES/DOWNVOTES.

So if there are 4 comments and 3 downvotes, it will count it like that

 $row['CountComment'] = 7;

But obviously, I don't want that. I want

$row['CountComment'] = 4;

Any ideas?

EDIT 1:

Here are the tables:

R_REGLE

CREATE TABLE `R_Regle` (
  `R_Id` int(11) NOT NULL AUTO_INCREMENT,
  `R_Auteur` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `R_Date` date NOT NULL,
  `R_Titre` varchar(150) CHARACTER SET latin1 NOT NULL,
  `R_Contenu` text CHARACTER SET latin1 NOT NULL,
  `R_Etape` int(11) NOT NULL DEFAULT '1' COMMENT '1 = discussion 2= VOTE',
  PRIMARY KEY (`R_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

C_COMMENTAIRE

CREATE TABLE `C_Commentaire` (
  `C_Id` int(11) NOT NULL AUTO_INCREMENT,
  `C_Date` datetime NOT NULL,
  `C_Pseudo` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `C_Contenu` text COLLATE utf8_unicode_ci NOT NULL,
  `C_IdRegle` int(11) NOT NULL,
  PRIMARY KEY (`C_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I_INTERET

CREATE TABLE `I_Interet` (
  `I_Id` int(11) NOT NULL AUTO_INCREMENT,
  `I_IdRegle` int(11) NOT NULL,
  `I_Pseudo` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `I_Up` tinyint(1) NOT NULL DEFAULT '0',
  `I_Down` tinyint(1) DEFAULT '0',
  PRIMARY KEY (`I_Id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

I can suggest using correlated query.. not sure if thats what you want since you didn't post any table structures its hard to know :

SELECT R_Id, R_Date, R_Titre, R_Contenu, R_Etape,
       (select COUNT(*) from C_Commentaire where C_Commentaire.C_IdRegle = R_Regle.R_Id) AS CountComment, 
       SUM(I_Interet.I_Up) AS Up ,
       SUM(I_Interet.I_Down) AS Down 
FROM R_Regle
        LEFT JOIN I_Interet ON I_Interet.I_IdRegle = R_Regle.R_Id 
GROUP BY R_ID

Write your query as below:

SELECT R_Regle.R_Id, R_Regle.R_Date, R_Regle.R_Titre, R_Regle.R_Contenu, R_Regle.R_Etape, 
COUNT(C_Commentaire.C_IdRegle) AS CommentCounts, 
SUM(I_Interet.I_Up) AS Up,
SUM(I_Interet.I_Down) AS Down 
FROM R_Regle
    LEFT JOIN C_Commentaire ON R_Regle.R_Id = C_Commentaire.C_IdRegle 
    LEFT JOIN I_Interet ON R_Regle.R_Id = I_Interet.I_IdRegle 
GROUP BY R_Regle.R_ID

In your query there are duplicate records arisen when you make a left join among the three tables.

The following query is one of few ways to achieve your desired result.

SELECT
    t.*,
    SUM(I_Interet.I_Up) AS Up,
    SUM(I_Interet.I_Down) AS Down
FROM
    (    
                SELECT 
                        R_Id,
                        R_Date,
                        R_Titre,
                        R_Contenu,
                        R_Etape,
                        COUNT(c_commentaire.C_IdRegle) countComment

                FROM
                R_Regle LEFT JOIN C_Commentaire ON C_Commentaire.C_IdRegle = R_Regle.R_Id
                GROUP BY R_Regle.R_Id ) t

LEFT JOIN I_Interet ON I_Interet.I_IdRegle = t.R_Id
GROUP BY t.R_Id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM