Php, MySql Sum from two tables

Alko

I'm trying to make this query work:

SELECT 
  IFNULL(SUM(days), 0) AS days 
FROM
  `table_days` 
WHERE task = 1 
GROUP BY task 
UNION
ALL 
SELECT 
  IFNULL(SUM(total), 0) AS total 
FROM
  `table_total` 
WHERE task = 1 
GROUP BY task ;

I have two tables :

1. table_days

    id      task    days    
    ==========================
    1       1       3.00
    2       1       2.00


2. table_total
    id      task    total   
    ==========================
    1       3       0.00

The query above partially works, the result is:

stdClass Object
(
    [days] => 5.00
)

but I would like to get the result from second table even if there are no records found. Something like

stdClass Object
(
    [days] => 5.00
    [total] => 0.00
)
alexander.polomodov

This code will work for you (example at sqlfiddle):

SELECT
  IFNULL(SUM(days), 0) AS value, 'days' as name
FROM
  `table_days`
WHERE task = 1
GROUP BY task
UNION ALL

(SELECT CASE WHEN u.value = -1 then 0 else value end as value, 'total' as name
    FROM
    (
        SELECT
          IFNULL(SUM(total), 0) AS value
        FROM
          `table_total`
        WHERE task = 1
        GROUP BY task
        UNION
        SELECT -1 as value
    ) u
);

And it will return:

+-------+-------+
| value | name  |
+-------+-------+
|     5 | days  |
|     0 | total |
+-------+-------+

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sum of two columns from two tables - MySQL

From Dev

Mysql Select with two SUM from two tables

From Dev

MYSQL sum from two different tables group by

From Dev

Json from two mysql tables in PHP

From Dev

PHP MYSQL Newsletter with addresses from two tables

From Dev

php, MySql subquery from two tables

From Dev

PHP and MySql - fetching rows from two tables

From Dev

Display data from two tables - PHP mySQL

From Dev

MySQL - Group By SUM with two tables

From Dev

Request from two tables with SUM

From Dev

sql sum from two tables

From Dev

Request from two tables with SUM

From Dev

[mysql][php] Problematic join results from two tables and filtering in php

From Dev

MySQL query from PHP combining data from two tables

From Dev

Sum of two columns from two tables

From Dev

Delete information from two join tables in MySQL using php

From Dev

Matching two columns from different tables - MySQL PHP

From Dev

PHP generate a multidimensional array from mysql two tables

From Dev

PHP login using details from two mysql tables

From Dev

Compare Two Tables, Get Value From Table and Multiply It - PHP MySQL

From Dev

Delete information from two join tables in MySQL using php

From Dev

Query syntax with php variables to select from two MySQL tables

From Dev

Create PHP two dimensional array from MySQL linked tables

From Dev

PHP generate a multidimensional array from mysql two tables

From Dev

Echo data from two different mysql tables using php

From Dev

Selecting data from two different tables of the same database in PHP/ MYSQL

From Dev

mysql join two tables, count and sum alias

From Dev

mySQL sum of two values in 2 different tables

From Dev

Left Join and SUM on two tables, MYSQL

Related Related

HotTag

Archive