Reputation: 6766
mysql - i have two tables as below
table1
city code, city name
1, chicago
2, miami
3, NY
table2
branch,branch name, city code, day, amt
50,a,1,jan, $10
32,b,2,feb, $30
i want to find branch and branch name where total amt in Jan was >$50 for brances in NY
i have code as below
select branch, branch name, sum(amt) from table2
where city code = (select city code from table1 where city name = 'NY')
and day="jan"
group by branch, branch name
having sum(amt)>50
NY
and chicago
then can i modify where clause aswhere city code in (select city code from table1 where city name in ('NY','chicago')
Upvotes: 0
Views: 19
Reputation: 780974
Your query #2 is correct.
The equivalent with a join is:
select table2.branch, table2.branch name, sum(table2.amt)
from table2
join table1 on table1.citycode = table2.citycode
where table2.day="jan"
and table1.cityname in ('NY', 'Chicago')
group by branch, branch name
having sum(amt)>50
In my experience, MySQL performs much better with the join than where in (subquery)
.
Upvotes: 2