If first query ( SELECT SUM(column) AS some_value FROM table1 WHERE column1 = 10; ) returns 5 and second query( SELECT column FROM table2 WHERE column1 = 10; ) returns 6 the result column should be 11;
I want to SUM the return values from two queries in one result by using single query;
antubis wrote:If first query ( SELECT SUM(column) AS some_value FROM table1 WHERE column1 = 10; ) returns 5 and second query( SELECT column FROM table2 WHERE column1 = 10; ) returns 6 the result column should be 11;
I want to SUM the return values from two queries in one result by using single query;
I try even the simplest: mysql> SELECT ( SELECT 1 + 1 ) + ( SELECT 2 + 2 ) AS sum;
Error:
ERROR 1064: You have an error in your SQL syntax. Check the manual that corresp
onds to your MySQL server version for the right syntax to use near 'SELECT 1 + 1
) + ( SELECT 2 + 2 ) AS sum' at line 1
antubis wrote:
First query is: SELECT SUM(column) AS some_value FROM table1 WHERE column1 = 10;
Second query is SELECT column FROM table2 WHERE column1 = 10;
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 5.0.20a-Debian_2-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
Database changed
mysql> create table test1(count int not null, primary key(count));
Query OK, 0 rows affected (0.01 sec)
mysql> create table test2(count int not null, primary key(count));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test1 values (100), (40);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into test2 values (60) , (100);
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> SELECT (SELECT SUM(count) FROM test1) + (SELECT SUM(count) FROM test2) AS sum;
+------+
| sum |
+------+
| 300 |
+------+
1 row in set (0.03 sec)