简体   繁体   中英

SQL get the sum of entries from join tables

The deeper i dive in mysql the more i lost and now i'm completed lost. So i have some tables:

MainTable

id|message|name
-----------------
1 |test   |OP
2 |test2  |jim
3 |test3  |ted

Table1

id|likes
---------
2 | 1
3 | 0

Table2

id|likes
---------
2 | 1

Table3

id|likes
---------
1 | 1
2 | 1
3 | 0

What i want to do is get the total number of likes (where a like is equal to 1) for every id in one column so i can count the total likes of a message(with its respective id).

Until now i have managed to join my tables so i get the a likes column at the end:

SELECT id,Table1.likes,Table2.likes,Table3.likes 
FROM MainTable
LEFT JOIN Table1.id ON MainTable.id = Table1.id LEFT JOIN Table2.id ON MainTable.id = Table2.id LEFT JOIN Table3.id ON MainTable.id = Table3.id

First of all,is it possible? I know my code is not great but at least its a start!

Thanks!

I am guessing you are looking for one of these...

SELECT
    id
    ,SUM(Table1.likes)
    ,SUM(Table2.likes)
    ,SUM(Table3.likes)
FROM MainTable
LEFT JOIN Table1 ON MainTable.id = Table1.id
LEFT JOIN Table2 ON MainTable.id = Table2.id
LEFT JOIN Table3 ON MainTable.id = Table3.id
GROUP BY MainTable.id

SELECT
    id
    ,SUM(Table1.likes)+SUM(Table2.likes)+SUM(Table3.likes)
FROM MainTable
LEFT JOIN Table1 ON MainTable.id = Table1.id
LEFT JOIN Table2 ON MainTable.id = Table2.id
LEFT JOIN Table3 ON MainTable.id = Table3.id
GROUP BY MainTable.id

Here is the query, counting non-zero likes for every row in MainTable:

SELECT 
    MainTable.id,
    MainTable.name,
    MainTable.message,
    COUNT(Table1.likes) + COUNT(Table2.likes) 
    + COUNT(Table3.likes) AS n_likes
FROM 
    MainTable
LEFT JOIN 
    Table1 ON MainTable.id = Table1.id
AND
    Table1.likes=1
LEFT JOIN
    Table2 ON MainTable.id = Table2.id
AND
    Table2.likes=1
LEFT JOIN
    Table3
ON
    MainTable.id = Table3.id
AND
    Table3.likes=1
GROUP BY
    MainTable.id;

fiddle

Beware, you have an error in your SQL syntax:

LEFT JOIN Table1.id

you must write the table name, without the column, when joining:

LEFT JOIN Table1

You can use UNION ALL to solve this.

See the below test.

CREATE  TABLE `test`.`items` (
  `id` INT NOT NULL ,
  `message` VARCHAR(45) NULL ,
  `name` VARCHAR(45) NULL ,
  PRIMARY KEY (`id`) );

insert into items values
(1, "Message 1", "OP"),
(1, "Message 2", "jim"),
(1, "Message 3", "ted");


CREATE  TABLE `test`.`table1` (
  `id` INT NOT NULL ,
  `likes` INT(10),
  PRIMARY KEY (`id`) );

CREATE  TABLE `test`.`table2` (
  `id` INT NOT NULL ,
  `likes` INT(10),
  PRIMARY KEY (`id`) );

CREATE  TABLE `test`.`table3` (
  `id` INT NOT NULL ,
  `likes` INT(10),
  PRIMARY KEY (`id`) );

insert into table1 values 
( 2, 1),
( 3, 0);

insert into table2 values 
( 2, 1),
( 3, 0);

insert into table3 values
(1,1),
(2,1),
(3,0);


select source.id, sum(likes)
from (
select id, likes from table1 as t1
UNION ALL
select id, likes from table2 as t2
UNION ALL
select id, likes from table3 as t3
) as source group by id;

Output

1, 1
2, 3
3, 0

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