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);
Related
Each individual query (t1 and t2) works fine by itself, however a simple join is seemingly not allowed.
I recreated the code using tables x1 and x2 and it was fine, however copy pasting t1 and t2 over the top gets me an error again.
It must be something to do with the variables?
problematic attempt:
select *
from
(
set #rownumber1 = 0;
select (#rownumber1:= #rownumber1 + 1) as num,
id,
updated_at as date,
risk_factor
from (select * from user.users_audit order by id) orig
order by id
) t1
left join
(
set #rownumber2 = 0;
select (#rownumber2:= #rownumber2 + 1) as num,
id,
updated_at as date,
risk_factor
from (select * from user.users_audit order by id) orig
order by id
) t2 on t1.id = t2.id
working attempt - but with tables I dont care about:
select *
from
(
select *
from sport_type_sources
) x1
inner join
(
select *
from sport_type_sources
) x2 on x1.id = x2.id
I would simply like to join the two tables.
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);
How can I set a column name for the output from bellow query ?
select
(select count(*) from t_table1 id = 1)
+
(select count(*) from t_table2 id = 1)
+
(select count(*) from t_table3 id = 1)
Use as:
select ( (select count(*) from t_table1 where id = 1) +
(select count(*) from t_table2 where id = 1) +
(select count(*) from t_table3 where id = 1)
) as col
Notice that I put the entire expression in parentheses. This is not required, but it makes the code more readable. I also fixed the subqueries.
If you want to run this multiple times, then a correlated subquery makes it easier to manage the ids:
select ( (select count(*) from t_table1 t where t.id = x.id) +
(select count(*) from t_table2 t where t.id = x.id) +
(select count(*) from t_table3 t where t.id = x.id)
) as col
from (select 1 as id) x;
Then, to modify the query, you only need to change the value in one place.
use as Keyword
select (select count(*) from t_table1 id = 1)+
(select count(*) from t_table2 id = 1)+
(select count(*) from t_table3 id = 1) as result
select sum(count_tab) as col_name from(
(select count(*) as count_tab from t_table1 id = 1)
union all
(select count(*) from t_table2 id = 1)
union all
(select count(*) from t_table3 id = 1))
(SELECT *, 0 AS user FROM table1)
UNION
(SELECT * FROM table2 WHERE unix >= {$threemonths})
ORDER BY unix DESC;
I need to add:
WHERE table2.identifier = table1.identifier or something
I want to get all from table1 and only rows from table2 where identifier is found in the results from table1's identifier column.
Please see if this works for you
(SELECT *, 0 AS user FROM table1)
UNION
(SELECT * FROM table2 WHERE unix >= {$threemonths} and exists (select 'Y' from table1 a where a.identifier = table2.identifier))
ORDER BY unix DESC;
Could be
SELECT *, 0 AS user FROM table1
UNION
SELECT * FROM table2 WHERE unix >= {$threemonths}
INNER JOIN table1 on table2.identifier = table1.identifier
ORDER BY unix DESC;
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