CURD的常用操作:

样式代码:

  1. // 增加四条数据
  2. insert staffs (name, gender, salary, email, birthday) values
  3. ('小王', 'male', 3440, 'abc@bb.com', '1999-11-11'),
  4. ('小李', 'female', 4550, 'cvb@cvb.con', '2000-11-11'),
  5. ('小白', 'male', 3566, 'nbn@bn.com', '1999-09-19'),
  6. ('小张', 'female', 6700, 'nsb@wd.com', '2001-01-21');
  7. // 删除小王
  8. delete from staffs where name = '小王';
  9. // 小白工资改为8566
  10. update staffs set salary = salary + 5000 where name = '小白';
  11. // 查看小白
  12. select name, gender, salary, email, birthday from staffs where name = '小白';

效果预览:

select查询:

样式代码:

  1. // 一. 分组查询: 按性别查询:
  2. select gender 性别,count(1) 数量 from staffs group by gender;

效果预览:

  1. // 二. 条件查询: 工资大于6000且性别为女
  2. select name,salary from staffs where salary>=6000 and gender = 'female';

效果预览:

  1. //三. 排序: 按工资进行排序
  2. select name,salary from staffs order by salary asc;
  3. // 降序: 按工资进行降序
  4. select name,salary from staffs order by salary desc;

效果预览:

  1. //四. 子查询式插入/复制插入
  2. insert staffs (name,gender, salary, email,birthday)
  3. (select name,gender, salary, email,birthday from staffs);

效果预览:

  1. // 五. 分页查询: 每页显示两条数据
  2. // 第一页: offset = ( 1 - 1 ) * 2 = 0
  3. select name,salary,email from staffs limit 2 offset 0;
  4. // 第二页: offset = ( 2 - 1 ) * 2 = 2
  5. select name,salary,email from staffs limit 2 offset 2;
  6. // 第三页: offset = ( 3 - 1 ) * 2 = 4
  7. select name,salary,email from staffs limit 2 offset 4;

效果预览:

  1. // 六. like: 查询name中以白为结尾的数据
  2. select name from staffs where name like '%白';
  3. // 查询name中第二个字为张的数据
  4. select name from staffs where name like '_张%';

效果预览:

关联操作:

样式代码:

  1. //先创建一个信息热榜再来一个栏目表
  2. // 信息热榜
  3. create table wzb (
  4. aid int unsigned not null auto_increment,
  5. title varchar(100) not null comment '文章标题',
  6. cid int unsigned not null comment '栏目 id',
  7. primary Key (aid)
  8. ) engine = innodb collate=utf8mb4_unicode_ci;
  9. -- ------------------------------------------
  10. insert wzb (title,cid) values
  11. ('有哪些给年轻人的忠告?',1),
  12. ('如何判断对方是个海王?',1),
  13. ('又遇跳槽季,在选择工作时有什么建议?', 2),
  14. ('校招c大概学习到什么程度?', 2);
  15. // 栏目表
  16. create table categories(
  17. cid int unsigned not null auto_increment,
  18. name varchar(100) not null comment '栏目名称',
  19. primary key (cid)
  20. ) engine = innodb collate=utf8mb4_unicode_ci;
  21. -- -------------------------------------------
  22. insert categories (name) value
  23. ('国内新闻'),('影视新闻'),('职场新闻'),('校园新闻');
  24. // 内连接
  25. select a.aid ,title, c.name from wzb as a, categories as c where a.cid = c.cid;
  26. // 简化
  27. select aid,title,name from wzb a, categories c where a.cid = c.cid;
  28. // 外连接: 左外连接,右外连接
  29. // 左外连接: 左表是主表,右边是从表
  30. select *
  31. from wzb a
  32. left join categories c
  33. on a.cid = c.cid;
  34. // 右外连接: 右表是主表,左边是从表
  35. select *
  36. from wzb a
  37. right join categories c
  38. on a.cid = c.cid;
  39. // 转内连接
  40. select *
  41. from wzb a
  42. right join categories c
  43. on a.cid = c.cid
  44. where a.aid is not null;
  45. // 将从表的为null的字段过滤掉
  46. // 自然连接: 是内连接的一个特例, 前提关联表中的存在同名字段,连using()都不要了, 如果用不到表别名,别名也不用了
  47. select aid,title,name from wzb natural join categories;

内连接效果预览:

左外连接效果预览:

右外连接效果预览:

转内连接效果预览:

自然连接效果预览:

预处理原理:

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

样式代码:

  1. // 生成预处理的sql语句
  2. prepare stmt from 'select name,salary from staffs where salary > ? limit ?';
  3. // 将真实的数据绑定到预处理语句中的占位符上 ?
  4. set @salary = 3300, @num = 3;
  5. execute stmt using @salary, @num;

效果预览:

更多相关文章

  1. MySQL的Hash Join能用吗?
  2. MySQL如何管理客户端连接?线程池篇
  3. MySQL如何管理客户端的连接?
  4. 为什么要远程连接Linux系统?linux操作系统入门
  5. 【Nest教程】连接MySQL数据库
  6. 配置文件中的数据库连接串加密了,你以为我就挖不出来吗?
  7. iOS图片预览、放大缩小
  8. CentOS7配置IP和远程连接
  9. 克隆虚拟机和相互登录

随机推荐

  1. 儿童视图不在angular-ui-router中工作
  2. jQuery延迟淡入时间超过预期
  3. word和.txt文件转html 及pdf文件, 使用poi
  4. 在HTML中添加<br /为何整篇内容却全部顶
  5. asp:Repeater 动态换行
  6. 是否可以将HTML元素固定到另一个的底部?
  7. ecshop如何实现鼠标滑过小图切换大图功能
  8. Jquery如何获取下一个控件的ID?
  9. Ajax技术--服务器返回数据格式(HTML,XML,J
  10. HTML5实现一个可编辑的模板页面