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;

更多相关文章

  1. 【体系课】数据可视化入门到精通 打造前端差异化竞争力
  2. 表数据量大读写缓慢如何优化(2)【查询分离】
  3. PMM配置监控PG数据库
  4. 利用AJAX实现一个无刷新的分页功能
  5. php学习笔记(类的别名引入与命名冲突的解决方案),数据库常用操作命
  6. 一份优秀的数据分析报告该具备什么条件?
  7. 第12章 0224 - 数据库操作基础2,学习心得、笔记(mySql的,CURD操作,
  8. 分布式数据库中间件设想
  9. SQL Server学习之路(一)

随机推荐

  1. ch010 Android GridView
  2. Android Email程序源码
  3. android内存机制
  4. Android(安卓)Jetpack系列——ViewModel
  5. Android ContentProvider
  6. Android: Android 3.0 SDK发布,速度更新之
  7. Android Bitmap内存优化
  8. android interview questions
  9. Android sharedUserId
  10. Android:Android官方培训课程中文版(v0.9