简体   繁体   中英

Select * as well as count/sum from another table

I am trying to write a MySQL query to select all rows from table a, as well as information from table b, while also querying the count and sum of values in another table c, for each row of a.

I will try to break that down a bit better, here is a simplified version of my tables:

Table A

+---------+----------+-----------+
| id      | name     |      bid  |
+---------+----------+-----------+
| 1       | abc      |         1 |
| 2       | def      |         1 |
| 3       | ghi      |         2 |
+--------------------------------+

Table B

+---------+----------+
| id      | name     |
+---------+----------+
| 1       | STAN     |      
| 2       | UCLA     | 
+--------------------+

Table C

+---------+----------+-----------+
| id      | aid      |   cnumber |
+---------+----------+-----------+
| 1       | 1        |        40 |
| 2       | 1        |        20 |
| 3       | 2        |        10 |
| 4       | 3        |        40 |
| 5       | 3        |        20 |
| 6       | 3        |        10 |
+--------------------------------+

What I need is a query that will return rows containing

a.id | a.name | b.id | b.name | SUM(c.cnumber) | COUNT(c.cnumber)

I am not sure if such a thing is even possible in MySQL.

My current solution is trying to query A + B Left Join and then UNION with A + C Right Join. It's not giving me the results I'm looking for however.

Thanks.

edit: current query re-written for this problem:

SELECT
 a.id,
 a.name,
 b.id,
 b.name
 "somecolumn" as dummy_column
 "somecolumn1" as dummy_column1
FROM a
LEFT OUTER JOIN b ON a.b.id = b.id
UNION
SELECT
 "somecolumn" as dummy_column
 "somecolumn1" as dummy_column1
 "somecolumn2" as dummy_column2
 "somecolumn3" as dummy_column3
COUNT(c.cnumber) AS ccount
SUM(c.cnumber) AS sum
FROM a
RIGHT OUTER JOIN c ON a.id = c.a.id; 

Unfortunately MySQL doesn't have FULL OUTER JOIN, and this is my temporary work around, although I don't think it's even the proper idea.

My desired output is all the rows from table A with some info from table B, as well as their totaled inputs in table C.

edit2:

        SELECT
          a.id,
          a.name,
          b.id,
          SUM(c.cnumber) as totalSum,
          (SELECT count(*) FROM c as cc WHERE cc.aid = a.id) as totalCount
        FROM
          a
        LEFT JOIN b ON a.bid = b.id
        LEFT JOIN c ON c.aid = a.id;

For future similar questions, solution:

        SELECT
          a.id AS aid,
          a.name,
          b.id,
          (SELECT SUM(c.rating) FROM c WHERE c.aid = aid) AS totalSum,
          (SELECT COUNT(c.rating) FROM c WHERE c.aid = aid) AS totalCount
        FROM
          a
        LEFT JOIN b ON a.bid = b.id;

Your query should be :-

SELECT
      a.id,
      a.name,
      b.id,
      (SELECT SUM(c.cnumber) FROM c as cd WHERE cd.aid = a.id) as totalSum,
      (SELECT count(*) FROM c as cc WHERE cc.aid = a.id) as totalCount
    FROM
      a
    LEFT JOIN b ON a.bid = b.id
    LEFT JOIN c ON c.aid = a.id;

It may help you.

I have tried your example.

  SELECT * ,
(SELECT SUM(cnumber) FROM `table_c` WHERE `table_c`.`iaid` = `table_a`.`id`),
(SELECT COUNT(cnumber) FROM `table_c` WHERE `table_c`.`a.id` = `table_a`.`id`) 
FROM `table_a`,`table_b` WHERE `table_b`.`id` = `table_a`.`b.id` 

i got the Following output .

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