mysqlmax多个字段_SQL中存在两个max条件的查询语句前段时间⼯作参加笔试,笔试题中有⼀道sql查询语句,条件是两个字段都是最⼤值,第⼀直觉是两个字段(例如age、hight)都等于
max(),⽤⼀个⾏⼦查询就⾏了。第⼆直觉⼜不是,如果表中恰好有⼀条age=max(age),hight=max(hight)的数据,⾏⼦查询是没问题的。但是如果age=max(age)的数据hight!=max(hight)并且hight=max(hight)的数据age=!max(age)呢?那么查出来的结果必然是空。
我想⾸先保证age=max(age),在age=max(age)的记录⾥查询hight=max(hight)的记录,这样应该能满⾜题意了吧?(后来我在其他地⽅看的题⽬好像就是要求第⼀直觉的那样0.0)然后呢?后⾯就复杂了,查询语句⼀层⼀层的嵌套......最后只是⼤致的草稿出来了,但是笔试时间到,⾯试官说算了,笔试题不重要......
flutter listview
今天⼜看到这个问题的⼀个例⼦(他是⽤⾏⼦查询做的),就把这个问题解决了。
wordpress免费企业主题mysql> create table maxtest(age int,hight int);
mysql面试题笔试Query OK,0 rows affected (0.27sec)
mysql> insert into maxtest values(30,180);
Query OK,1 row affected (0.05sec)javascript数组最大值
mysql> insert into maxtest values(30,173);
Query OK,1 row affected (0.06sec)
mysql> insert into maxtest values(32,193);
Query OK,1 row affected (0.06sec)
arm嵌入式系统教程张石
mysql> insert into maxtest values(23,199);
Query OK,1 row affected (0.06sec)
mysql> select * frommaxtest;+------+-------+
| age | hight |
+------+-------+
| 30 | 180 |
| 30 | 173 |
| 32 | 193 |
| 23 | 199 |
+------+-------+
4 rows in set (0.00sec)
mysql> select * from maxtest where (age,hight)=(select max(age),max(hight) frommaxtest);
Emptyset (0.00sec)
mysql> select * from maxtest where hight=(select max(hight) from (select * from maxtest where age=(select max(age) frommaxtest)) a);+------+-------+
从git上下载代码到本地| age | hight |
+------+-------+
| 32 | 193 |
+------+-------+
1 row in set (0.00sec)
java jdbc查询10000次:useTime=22988、24231
另外⼀种写法:
select * from maxtest where age=(select max(age) from maxtest) and hight=(select max(hight) from (select max(age) from maxtest) a);
这种写法可能更容易理解⼀些
java jdbc查询10000次:useTime=20527、***

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。