登录  /  注册
博主信息
博文 46
粉丝 0
评论 0
访问量 37749
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
练习CURD常用操作,select查询与关联操作,预处理原理
lus菜
原创
846人浏览过

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;

效果预览:

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学