I have 3 tables. I want to select count in 1 result, like:
table1=1000 records + table2=400 records + table3=200 records = 1600
1600 is the one result I want back from the server.
MySQL inner join perhaps? Any suggestions?
Try this :
select sum(c) from (
select count(*) as c from table1
union
select count(*) as c from table2
union
select count(*) as c from table3
) tmp
That'll give you the total.
select
(
select count(columnname) from table1
) + (
select count(columnname) from table2
)+ (
select count(columnname) from table3
)
try this...,
SELECT (SELECT COUNT(*) FROM tbl1
)+
(SELECT COUNT(*) FROM tbl2
)+
( SELECT COUNT(*) FROM tbl3
) as 'AllCount'
select
((select count(*) from table1) + (select count(*) from table2) + (select count(*) from table3))
as totalCount;
Try this:
SELECT ((SELECT COUNT(*) FROM tbl1 ) + (SELECT COUNT(*) FROM tbl2 ) + (SELECT COUNT(*) FROM tbl3 )) AS TotalRecords
Thank you all for your responses I have 3 tables and i want to select a count in 1 single result
still i get results back like this
count1 count2 count3
1235 134 234
and this is not what i want a total one result
Related
I have a query like :
SELECT COUNT(DISTINCT `ID`) FROM db1.table UNION ALL SELECT COUNT(DISTINCT `ID`) FROM db2.table
My real query is more complicate, i have LEFT JOIN and multiple condition etc...
This query return to me an array with 2 results : Count 1 and Count 2
How can i only return one result ? Count 1 + Count 2 ?
Thanks !
Try this:
SELECT (SELECT COUNT(DISTINCT `ID`) FROM db1.table) +
(SELECT COUNT(DISTINCT `ID`) FROM db2.table)
If you also want to return separate db1, db2 counts, then use this query:
SELECT countDb1, countDb2, countDb1 + countDb2 AS total
FROM (
SELECT (SELECT COUNT(DISTINCT `ID`) FROM db1.table) AS countDb1,
(SELECT COUNT(DISTINCT `ID`) FROM db2.table) AS countDb2) AS t
I have below two query's SUM the values
Query1:*
SELECT SUM(price) FROM TABLE1 WHERE acc_id = '555'
Query2:
SELECT SUM(price) FROM TABLE2 WHERE account = '555' && active='1'
I try to combine this two query but give wrong sum result , for example if query1 sum is: -86500 and Query2 sum is: 76000 , RESULT must be -10500 but result shown with a number like -486000
I'm trying like this, but i'm not getting expected result.
SELECT SUM(t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
Table image :
Due to join the number of records get duplicated and you get a higher value for sum
try this
SELECT sum(prc)
FROM (
SELECT SUM(price) prc FROM TABLE1 WHERE acc_id = '555'
union all
SELECT SUM(price) prc FROM TABLE2 WHERE account = '555' && active='1'
) a
Try this
SELECT SUM(C.TOTAL) AS TOTAL_CREDIT FROM (SELECT SUM(A.price) AS TOTAL FROM TABLE1 A WHERE A.acc_id = '555'
UNION ALL
SELECT SUM(B.price) AS TOTAL FROM TABLE2 B WHERE B.account = '555' && B.active='1') C;
try that
SELECT (SUM(t1.price) + SUM(t2.price)) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
try this
SELECT (t1.price + t2.price) AS TotalCredit
FROM TABLE1 AS t1, TABLE2 AS t2
WHERE t1.`acc_id` = '555' && t2.`Account`='555' && t2.`Active`='1'
EDIT:
here what you looking for i think
SELECT (SUM(t1.price)+SUM(t2.price) )/2 AS TotalCredit
FROM Table1 AS t1, Table2 AS t2
WHERE t1.`acc_id` = '555' && t2.`account`='555' && t2.`active`='1'
DEMO FIDDLE HERE
How about this:
SELECT SUM(a)
FROM
(SELECT SUM(price) AS a
FROM TABLE1
WHERE acc_id = '555'
UNION ALL
SELECT SUM(price) AS a
FROM TABLE2
WHERE account = '555' && active='1')
Join could be better. :) Would be even better if you could have showed us the table schema. Here is a solution based on some assumed sample data.
Sample data:
Table1:
ID PRICE
11 200
55 300
33 200
44 100
55 500
Table2:
ID PRICE ACTIVE
1 200 0
2 300 1
55 200 0
55 100 1
55 400 1
SQLFIDDLE DEMO
Query:
select sum(t.price) + x.tb2credit
from tb1 as t
inner join
(SELECT id, SUM(price) AS Tb2Credit
FROM tb2
WHERE id = 55
and `Active`=1) x
on t.id = x.id
Results:
SUM(T.PRICE) + X.TB2CREDIT
1300
SELECT DISTINCT ID FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4 ORDER BY total_hits DESC) AS sumtbl LIMIT 50;
This query works fine and selects unique ID's ordered by total_hits DESC, the question is how can I return total_hits column too having Id's unique?
SELECT ID, SUM(total_hits)
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
) AS sumtbl
GROUP BY ID
ORDER BY SUM(total_hits) DESC
LIMIT 50;
If you want the total_hits FROM all the ids across all the tables, you'll need to do a sum / group by. Not sure if this is what you're asking since the question is vague...
SELECT DISTINCT id, sum(total_hits) total_hits
FROM (SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4) AS sumtbl
GROUP BY id
ORDER BY total_hits DESC
LIMIT 50
Also, don't use select * as it's bad practice, esp. if you go to add a column to one of those tables and not the others, your whole query will break.
UPDATE
In your case you can do
SELECT ID, MAX(total_hits) as max_hits
FROM (
SELECT * FROM tbl1 UNION
SELECT * FROM tbl2 UNION
SELECT * FROM tbl3 UNION
SELECT * FROM tbl4
)sumtbl
GROUP BY ID
ORDER BY max_hits DESC
LIMIT 50
Note : you don't need ORDER BY in derived query, it goes to upper
Also, it would be better, if you
SELECT id, total_hits
FROM tbl1
UNION ALL
SELECT id, total_hits FROM tbl2 etc. rather then selecting all fields from tables in your derived query.
I have two tables that I count rows of them. For example;
Select count(*) FROM tbl_Events
Select count(*) FROM tbl_Events2
I need total count. How can I sum the result with a single statement?
select sum(cnt) from (
select count(*) as cnt from tbl_events
union all
select count(*) as cnt from tbl_events2
) as x
Try this:
SELECT (Select count(*) FROM tbl_Events) + (Select count(*) FROM tbl_Events2)
Or (tested in MSSQL), this:
SELECT COUNT(*)
FROM (SELECT * FROM tbl_Events
UNION ALL
SELECT * FROM tbl_Events2) AS AllEvents
I'd guess the first will lead to better performance because it has more obvious index options. Test to be sure, though.
Select Count(*)
From(
Select * From tbl_Events
Union All
Select * From tbl_Events2) as A
How can I sum the columns of 3 tables?
I have table1, table2, and table3 with the column 'revenue'.
I can do SELECT SUM(REVENUE) FROM TABLE1 but what do i do for all of them?
I tried: SELECT SUM(
table1.Revenue+
table2.Revenue +
table3.Revenue
)
FROM
table1,
table2,
table3' but it doesn't work...
Any ideas? Thanks!
select sum(rev) as trev
from
(
SELECT SUM( Revenue) as rev FROM table1
union all
SELECT SUM( Revenue) as rev FROM table2
union all
SELECT SUM( Revenue) as rev FROM table3
) as tmp
You need to add all of the individual sums together:
SELECT (SUM(table1.Revenue) + SUM(table2.Revenue) + SUM(table3.Revenue))
AS total_rev FROM table1, table2, table3
select sum( revenue )
from (
select revenue from table1
union
select revenue from table2
union
select revenue from table3
)
SELECT SUM(REVENUE) FROM (
SELECT REVENUE FROM TABLE1
UNION ALL
SELECT REVENUE FROM TABLE2
UNION ALL
SELECT REVENUE FROM TABLE3
) revenues