Skip to content

Commit 497eead

Browse files
committed
子查询匹配两个值
1 parent 0b329e1 commit 497eead

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
**************************************************************************************************
2+
将子查询改为join写法时要注意表中关系是不是一对多或多对多,这种情况下要对查询结果使用distinct去重。
3+
4+
**************************************************************************************************
5+
select a.user_name,b.timestr,kills from user1 a join user_kills b on
6+
a.id=b.user_id join(select user_id,max(kills) as cnt from user_kills group by user_id)c on
7+
b.user_id=c.user_id and b.kills=c.cnt;
8+
9+
mysql中独有的列过滤方式:多列过滤
10+
select a.user_name,b.timestr,kills
11+
from user1 a
12+
join user_kills b on a.id=b.user_id
13+
where (b.user_id,b.kills) in (
14+
select user_id,max(kills)
15+
from user_kills
16+
group by user_id
17+
);
18+

0 commit comments

Comments
 (0)