mysql-我有如下两个表
表1
city code, city name
1, chicago
2, miami
3, NY
表2
branch,branch name, city code, day, amt
50,a,1,jan, $10
32,b,2,feb, $30
我想查找分支机构和分支机构名称,其中纽约的brances 1月份的amt总额大于50美元
我的代码如下
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
和chicago
,那么我可以将where子句修改为 where city code in (select city code from table1 where city name in ('NY','chicago')
答案 0 :(得分:2)
您的查询2是正确的。
具有联接的等效项是:
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
根据我的经验,MySQL在连接方面的表现要比where in (subquery)
好。