附件:对mysql知识点描述比较全的博客
https://blog.csdn.net/laodanqiu/article/details/131563200
原题
为方便表达,将创建两个表
mysql> select * from customer;
+-------------+---------------+----------+---------------+
| customer_id | customer_name | tel | customer_type |
+-------------+---------------+----------+---------------+
| 1 | 张三 | 33333333 | 普通 |
| 2 | 李四 | 44444444 | 普通 |
| 3 | 王五 | 55555555 | VIP |
| 4 | 李六 | 66666666 | VIP |
| 5 | 李八 | 88888888 | 普通 |
+-------------+---------------+----------+---------------+
mysql> select * from orders;
+----------+-------------+---------------------+--------+
| order_id | customer_id | order_time | money |
+----------+-------------+---------------------+--------+
| 1 | 2 | 2010-08-01 00:00:00 | 466.00 |
| 2 | 2 | 2010-08-02 00:00:00 | 555.00 |
| 3 | 3 | 2010-09-01 00:00:00 | 200.00 |
| 4 | 4 | 2010-09-01 00:00:00 | 300.00 |
| 5 | 4 | 2010-09-02 00:00:00 | 500.00 |
| 6 | 5 | 2010-09-20 00:00:00 | 222.00 |
+----------+-------------+---------------------+--------+
1.新增记录,订单表中添加1条数据,订单ID7,客户ID5,时间2010-09-20,金额100.00
insert into `orders` (`customer_id`,`order_time`,`money`)
values (5,'2010-09-20','100.00');
mysql> select * from orders;
+----------+-------------+---------------------+--------+
| order_id | customer_id | order_time | money |
+----------+-------------+---------------------+--------+
| 1 | 2 | 2010-08-01 00:00:00 | 466.00 |
| 2 | 2 | 2010-08-02 00:00:00 | 555.00 |
| 3 | 3 | 2010-09-01 00:00:00 | 200.00 |
| 4 | 4 | 2010-09-01 00:00:00 | 300.00 |
| 5 | 4 | 2010-09-02 00:00:00 | 500.00 |
| 6 | 5 | 2010-09-20 00:00:00 | 222.00 |
| 7 | 5 | 2010-09-20 00:00:00 | 100.00 |
+----------+-------------+---------------------+--------+
2.删除记录,删除没有订单的客户表记录
delete from `customer` where customer_id not in
(select distinct(customer_id) from orders )
mysql> select * from customer;
+-------------+---------------+----------+---------------+
| customer_id | customer_name | tel | customer_type |
+-------------+---------------+----------+---------------+
| 2 | 李四 | 44444444 | 普通 |
| 3 | 王五 | 55555555 | VIP |
| 4 | 李六 | 66666666 | VIP |
| 5 | 李八 | 88888888 | 普通 |
+-------------+---------------+----------+---------------+
3.修改时据,所有订单,累计订单金额大于300元的客户,客户类型改为VIP
update `customer` set `customer_type`= 'VIP'
where customer_id in
( select customer_id from `orders`
group by customer_id having sum(money) > 300)
mysql> select * from customer;
+-------------+---------------+----------+---------------+
| customer_id | customer_name | tel | customer_type |
+-------------+---------------+----------+---------------+
| 2 | 李四 | 44444444 | VIP |
| 3 | 王五 | 55555555 | VIP |
| 4 | 李六 | 66666666 | VIP |
| 5 | 李八 | 88888888 | VIP |
+-------------+---------------+----------+---------------+
4.复合查询,统计客户类型的合计金额,按客户类型,分组求和
上述处理完后,表中仅剩VIP的客户类型,为查看结果,将客户id为5的客户类型修改为“普通”。修改后的表如下:
mysql> select * from customer;
+-------------+---------------+----------+---------------+
| customer_id | customer_name | tel | customer_type |
+-------------+---------------+----------+---------------+
| 2 | 李四 | 44444444 | VIP |
| 3 | 王五 | 55555555 | VIP |
| 4 | 李六 | 66666666 | VIP |
| 5 | 李八 | 88888888 | 普通 |
+-------------+---------------+----------+---------------+
select c.customer_type,sum(o.money) as '总金额' from
customer as c,orders as o
where c.customer_id = o.customer_id
group by c.customer_type
+---------------+---------+
| customer_type | 总金额 |
+---------------+---------+
| VIP | 2021.00 |
| 普通 | 322.00 |
+---------------+---------+
总结:对于面试过程中出现的mysql题目,还是需要私下多练,提升熟练度,并灵活应用;在面试答题中保持冷静,看清题目,比如第三题的 “累计” 等要特别注意。一般题目难度是逐层提高,如果感觉后面难度降低,须认真再次查看一遍!!!
扩展
对于复杂查询语句中,语句中的查询顺序
select c.customer_name as '姓名',sum(o.money) as '总金额' from customer c
inner join orders o
on c.customer_id = o.customer_id
where c.customer_type = 'VIP'
group by c.customer_name
having sum(o.money) > 200
order by '总金额' asc
limit 1
其实,引擎在执行上述每一步时,都会在内存中形成一张虚拟表,然后对虚拟表进行后续操作,并释放没用的虚拟表的内存,以此类推。
具体解释:(注:下面“VT”表示 → 虚拟表 virtual )
1.from:select * from table_1, table_2; 与 select * from table_1 join table_2; 的结果一致,都是表示求笛卡尔积;用于直接计算两个表笛卡尔积,得到虚拟表VT1,这是所有select语句最先执行的操作,其他操作时在这个表上进行的,也就是from操作所完成的内容
2.on: 从VT1表中筛选符合条件的数据,形成VT2表;
join: 将该 join 类型的数据补充到VT2表中,例如 left join 会将左表的剩余数据添加到虚表VT2中,形成VT3表;若表的数量大于2,则会重复1-3步;
3.where: 执行筛选,(不能使用聚合函数)得到VT4表;
4.group by: 对VT4表进行分组,得到VT5表;其后处理的语句,如select,having,所用到的列必须包含在group by条件中,没有出现的需要用聚合函数;
5.having: 筛选分组后的数据,得到VT6表;
6.select: 返回列得到VT7表;
7.distinct: 用于去重得到VT8表,该项为获取结果的处理,例如聚合函数,重命名等;
8.order by: 用于排序得到VT9表;
9.limit: 返回需要的行数,得到VT10;
总结:from->join->where->group by->having->select->内容处理->order by->limit