I'm currently trying to sum up the result of an MySQL statement which gives me the amount of rows of several different tables.
I already studied every solution of stackoverflow concerning this matter. Unfortunately I'm not able to figure out the correct syntax for my case.
My working statement with the results not summed up:
SET #shipmentId=456;
SELECT
(SELECT COUNT(*) FROM Table1 WHERE ShipmentID = #shipmentId)
UNION ALL
(SELECT COUNT(*) FROM Table2 WHERE ShipmentID = #shipmentId)
UNION ALL
(SELECT COUNT(*) FROM Table3 WHERE ShipmentID = #shipmentId)
UNION ALL
(SELECT COUNT(*)FROM Table4 WHERE ShipmentID = #shipmentId)
UNION ALL
(SELECT COUNT(*)FROM Table5 WHERE ShipmentID = #shipmentId);
In my opinion this should be the nearest to a correct solution but I'm not sure (tried a lot of variants):
SET #shipmentId=456;
SELECT SUM(MeasuredValues) FROM
SELECT(
(SELECT COUNT(*) FROM Table1 WHERE ShipmentID = #shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*) FROM Table2 WHERE ShipmentID = #shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*) FROM Table3 WHERE ShipmentID = #shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*)FROM Table4 WHERE ShipmentID = #shipmentId) AS MeasuredValues
UNION ALL
(SELECT COUNT(*)FROM Table5 WHERE ShipmentID = #shipmentId) AS MeasuredValues) t1;
Thanks in advance for your help.
Already tried the following links:
StackOverFlow Solution1
StackOverFlow Solution2
You could just simply sum up your selects:
SET #shipmentId=456;
SELECT
(SELECT COUNT(*) FROM Table1 WHERE ShipmentID = #shipmentId)
+
(SELECT COUNT(*) FROM Table2 WHERE ShipmentID = #shipmentId)
+
(SELECT COUNT(*) FROM Table3 WHERE ShipmentID = #shipmentId)
+
(SELECT COUNT(*) FROM Table4 WHERE ShipmentID = #shipmentId)
+
(SELECT COUNT(*) FROM Table5 WHERE ShipmentID = #shipmentId);
Related
I have 2 identical dead-simple tables in a MySQL database with different data. I need a single query that will return all the results that aren't a duplicate.
Here's an example:
Table 1. (column "item")
a
b
c
d
Table 2. (column "item")
a
b
e
f
x
Wanted Result
c
d
e
f
x
Try this -
SELECT * FROM TABLE1 WHERE ITEM NOT IN (SELECT ITEM FROM TABLE2)
UNION
SELECT * FROM TABLE2 WHERE ITEM NOT IN (SELECT ITEM FROM TABLE1)
You can use NOT EXISTS, e.g.:
SELECT item
FROM table1 t1
WHERE NOT EXISTS (
SELECT item FROM table2 WHERE item = t1.item
);
We could join them using a distinct an union ALL. then count and having.
the distinct is needed since we care about unique accross the sets.
SELECT item FROM (
SELECT distinct item
FROM tbl1
UNION ALL
SELECT distinct item
FROM tbl2) B
GROUP BY item
HAVING count(*) =1
SELECT * FROM TABLE1 WHERE ITEM NOT IN (SELECT ITEM FROM TABLE2)
union
SELECT * FROM TABLE2 WHERE ITEM NOT IN (SELECT ITEM FROM TABLE1)
Or another method would be to use an inner join to grab all the duplicate data and then pull all the data that isn't in your duplicate dataset.
SELECT * into #temptable FROM TABLE1 INNER JOIN TABLE2 on table2.x = table1.x
union
SELECT * into #temptable FROM TABLE2 INNER JOIN TABLE1 on table2.x = table1.x
SELECT * FROM Table1 WHERE NOT IN (SELECT * FROM #temptable)
UNION
SELECT * FROM Table2 WHERE NOT IN (SELECT * FROM #temptable)
A lot like xQbert's, but with the assumption that item is UNIQUE/PRIMARY...
SELECT a.*
FROM
( SELECT item FROM table1
UNION ALL
SELECT item FROM table2
) a
GROUP
BY item
HAVING COUNT(*) = 1;
Writing down this using JOIN ..how?
because this is very slow..
SELECT *
FROM table1
WHERE ID IN (SELECT ID
FROM table1
GROUP BY ID
HAVING COUNT(*) = 2
AND MAX(awaiting) = 1)
AND awaiting = 1
so, how can I write?
Here is the join version:
SELECT t1.*
FROM table1 t1 join
(SELECT ID
FROM table1
GROUP BY ID
HAVING COUNT(*) = 2 AND MAX(awaiting) = 1
) tsum
on t1.id = tsum.id
WHERE t1.awaiting = 1
SELECT t1.*
FROM table1 AS t1
INNER JOIN
(
SELECT ID
FROM table1
GROUP BY ID
HAVING COUNT(*) = 2
AND MAX(awaiting) = 1
) AS t2 ON t1.ID = t1.ID AND t1.awaiting = t2.awaiting
WHERE t1.awaiting = 1;
I guess awaiting is 0 or 1. If so in your inner query MAX(awaiting) = 1 is redundant because of WHERE statement awaiting = 1
Also in this case you can use the following query.
SELECT *
FROM table1 as T1
WHERE
awaiting = 1
AND
(SELECT count(*) FROM table1 WHERE ID=T1.ID)=2
I have a query like this.
SELECT count(*)
FROM table1 e
WHERE e.column1=1
AND e.id IN
(SELECT MAX(ID)
FROM table2 A
WHERE A.column1=1
AND A.date=CURDATE()
GROUP BY A.column2);
When I run this query it is taking too much of time as I am having thousands of records. How can I tune the query to perform better.
Thanks in advance.
EDIT: column2 in table2 is id of Table1
Change in (. . .) To use join instead. Like
SELECT count(*)
FROM table1 AS e
Inner join
(
SELECT MAX(ID)
FROM table2 A
WHERE A.column1 = 1
AND A.date = CURDATE()
GROUP BY A.column2
) t2 on e.id = t2.id
WHERE e.column1 = 1
Maybe:
SELECT count(*)
FROM table1 e
WHERE e.column1=1
AND EXISTS
(SELECT *
FROM table2 A
WHERE A.column1=1
AND A.date=CURDATE()
AND A.ID = e.id);
Imagine:
t1 = 1
t2 = 3
t3 = 5
I need to run individual selects on each table, and report the count in a single amount, ie:
select *
from (select count(*) from t1)
+ (select count(*) from t2)
+ (select count(*) from t3);
My end result should be = 9
You're pretty close; you can write:
select (select count(*) from t1)
+ (select count(*) from t2)
+ (select count(*) from t3)
;
select
(select count(*) from t1)
+ (select count(*) from t2)
+ (select count(*) from t3);
how can i use the result of a subquery inside its parent query?
my code look like this.
select (select count(*) from tbl1 group by field) as result1,
(select count(*) from tbl2 group by field) as result2,
(select count(*) from tbl3 group by field) as result3,
result1 + result2 - result3 as total1,
result1 + result2 as total2
from tbl4 ;
some suggest to direct add the subquery .
(select count(*)*....) as result1 + (select count(*)*....) as result2
but i think its not want i need, bcoz i need also to display/maintain the value of result1 in my result.
is there somebody who can help me, i badly need this to work out, i appreciate every help.
thanks in advance.
Suppose your sub query will return a set. So just use the following---
SELECT column1, column2, column3
FROM table1
WHERE column3 IN (SELECT column3
FROM table2 WHERE condition);
select count1, count2, count3,
count1+count2-count3 as total1, count1+count2 as total2
from tbl4
inner join
(select count(*) as count1,name from tbl1 group by name)as result1
on tbl4.name=result1.name
inner join
(select count(*)as count2,name from tbl2 group by name) as result2
on tbl4.name=result2.name
inner join
(select count(*)as count3,name from tbl3 group by name) as result3
on tbl4.name=result3.name
please try this query and let me know
select result1.cnt,result2.cnt,result2.cnt,result1.cnt +
result2.cnt - result3.cnt as total1,
result1.cnt + result2.cnt as total2
from
(select field,COUNT(*) cnt from tbl1 group by field) as result1
join
(select field,COUNT(*) cnt from tbl2 group by field) as result2
on result1.field=result2.field
join
(select field,COUNT(*) cnt from tbl3 group by field) as result3
on result3.field=result2.field
join tbl4
on tbl1.field = tbl4.field
You would have to wrap your query in a subselect to be able to access the aliased column names:
SELECT a.result1 + a.result2 - a.result3 AS total1,
a.result1 + a.result2 AS total2
FROM (
select (select count(*) from tbl1 group by field) as result1,
(select count(*) from tbl2 group by field) as result2,
(select count(*) from tbl3 group by field) as result3
) a
But this would actually be a much better solution:
SELECT a.cnt + b.cnt - c.cnt AS total1,
a.cnt + b.cnt AS total2
FROM (SELECT COUNT(*) cnt FROM tbl1 GROUP BY field) a
CROSS JOIN (SELECT COUNT(*) cnt FROM tbl2 GROUP BY field) b
CROSS JOIN (SELECT COUNT(*) cnt FROM tbl3 GROUP BY field) c