您的位置:首页 > 职场人生

mysql面试题以及答案包括优化

2016-09-20 00:03 537 查看
1、用一条SQL语句 查询出每门课都大于80分的学生姓名

id name  subject  score

1    张三    语文      81

2    张三    数学      75

3    李四    语文      76

4    李四    数学      90

5    王五    语文      81

6    王五    数学      100

7    王五    英语      90

创建表:

mysql> create table score(id int primary key, name varchar(10), subject varchar(20), score int ) engine=innodb;
Query OK, 0 rows affected

mysql> insert into score select 1,'张三','语文',81;
mysql> insert into score select 2,'张三','数学',75;
mysql> insert into score select 3,'李四','语文',76;
mysql> insert into score select 4,'李四','数学',90;
mysql> insert into score select 5,'王五','语文',81;
mysql> insert into score select 6,'王五','数学',100;
mysql> insert into score select 7,'王五','英语',90;




答案:

mysql> select name from (select name,count(*) as tcount from score where score > 80 group by name) as t where t.tcount in (select count(*) from score group by name);
+------+
| name |
+------+
| 王五 |
+------+
1 row in set

查询计划:

mysql> explain extended select name from (select name,count(*) as tcount from score where score > 80 group by name) as t where t.tcount in (select count(*) from score group by name);
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 3 | 100 | Using where |
| 3 | SUBQUERY | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using temporary; Using filesort |
| 2 | DERIVED | score | ALL | NULL | NULL | NULL | NULL | 7 | 100 | Using where; Using temporary; Using filesort |
+----+-------------+------------+------+---------------+------+---------+------+------+----------+----------------------------------------------+
3 rows in set

mysql> show warnings;
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | select `t`.`name` AS `name` from (select `demo`.`score`.`name` AS `name`,count(0) AS `tcount` from `demo`.`score` where (`demo`.`score`.`score` > 80) group by `demo`.`score`.`name`) `t` where <in_optimizer>(`t`.`tcount`,`t`.`tcount` in ( <materialize> (select count(0) AS `count(*)` from `demo`.`score` group by `demo`.`score`.`name` ), <primary_index_lookup>(`t`.`tcount` in <temporary table> on distinct_key where ((`t`.`tcount` = `materialized subselect`.`count(*)`))))) |
+-------+------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

mysql>
 

2、mysql删除重复记录

id  name

1    aa

2    aa

3    cc

4    cc

创建表:

mysql> create table t(id int primary key,name varchar(10))engine=innodb;
Query OK, 0 rows affected
mysql> insert into t select 1,'aa';
mysql> insert into t select 2,'aa';
mysql> insert into t select 3,'cc';
mysql> insert into t select 4,'cc';




答案:

mysql> delete from t where id in(select * from (select max(id) from t group by name having count(*)>1)temp);
Query OK, 2 rows affected
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: