博客列表 >MYSQL数据库CURD中常用操作命令汇总及代码演示

MYSQL数据库CURD中常用操作命令汇总及代码演示

庖丁
庖丁原创
2021年03月03日 08:33:21710浏览

1. CURD中常用操作

1.1插入

  1. // mysql插入数据方法
  2. insert yuangong(name,gender,salary,email,birthday)
  3. values('张三','male',5000,'zhangsan@163.com','1986-02-01');
  4. //mysql插入列方法
  5. alter table yuangong add age int unsigned not null default 0 comment '年龄' after gender;

1.2删除

  1. //删除某条数据
  2. delete from yuangong where yid = 4;
  3. //清空表
  4. truncate yuangong;
  5. //删除表
  6. drop table yuangong;

1.3修改或者更新

  1. //mysql更新某列数据(示例为根据生日更新年龄)
  2. update yuangong set age = timestampdiff(year,birthday,now());
  3. //根据条件更新某一个人的字段信息
  4. update yuangong set gender = 'female' where name = '张三';
  5. //修改自增起始序号
  6. alter table yuangong auto_increment = 4;

1.4查询操作

  1. //查看当前数据库
  2. select database();
  3. //查看当前数据库的版本
  4. select version();
  5. //查看当前时间
  6. select now();
  7. //条件查询-查询年龄大约35的员工相关信息
  8. select name,gender,age,salary from yuangong where age > 35;
  9. //条件查询-区间查询
  10. select name,gender,age,salary from yuangong where age between 34 and 37;
  11. //分组查询-如果使用中文别名必须复制到命令行,不支持手写中文别名(可以省略as关键字不写)
  12. //聚合函数 max() min() sum() avg() count()
  13. select gender as '性别',count(gender) as '数量' from yuangong group by gender;
  14. //只查询男员工的数量,分组细分条件只能用having
  15. select gender as '性别',count(gender) as '数量' from yuangong group by gender having gender = 'male';
  16. //排序查询(asc为升序,desc为降序)
  17. select name,gender,age,salary from yuangong order by age asc;
  18. //分页查询 偏移量 =(page - 1)*10
  19. //第1页
  20. select name,gender,age,salary from yuangong limit 10 offset 0;
  21. //第2页
  22. select name,gender,age,salary from yuangong limit 10 offset 10;
  23. //第3页
  24. select name,gender,age,salary from yuangong limit 10 offset 20;
  25. //子查询
  26. select * from yuangong where age = (select max(age) from yuangong);
  27. //集合查询 in
  28. select * from yuangong where yid in(1,3);
  29. //模糊查询 like
  30. //名字以张开头的数据,%表示任何字符出现任意次数。
  31. select * from yuangong where name like '张%';
  32. //名字第二个字符为"五"的数据,下划线_表示匹配单个字符。
  33. select * from yuangong where name like '_五%';
  34. //is null(空值) is not null(非空值)
  35. select * from yuangong where name is not null;
  36. //通过多列数据过滤 and
  37. select * from yuangong where age >35 and gender = 'female';
  38. //匹配任意条件的数据 or
  39. select * from yuangong where age = 35 or age = 37;
  40. //关联查询
  41. //内连接
  42. select name,gender,age,salary
  43. from yuangong join gongsi
  44. using(cid)
  45. where cid = 1
  46. //左外连接
  47. select name,gender,age,salary
  48. from yuangong as y
  49. left join gongsi as g
  50. using(cid)
  51. where y.yid is not null
  52. //右外连接
  53. select name,gender,age,salary
  54. from yuangong as y
  55. right join gongsi as g
  56. using(cid)
  57. where g.gid is not null
  58. //自然连接(前提是关联表中存在同名字段)
  59. select name,gender,age,salary
  60. from yuangong yid join gongsi

2. 预处理原理

SQL语句中的数据,只有在执行阶段再与字段进行绑定。
作用:防止SQL注入攻击

  1. //生成预处理的sql语句
  2. prepare stmt from 'select * from yuangong where age >? and gender = ?';
  3. //将真实的数据绑定到占位符上,要注意顺序
  4. set @age = 35,@gender = 'female';
  5. execute stmt using @age, @gender;
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议