搜索
博主信息
博文 40
粉丝 0
评论 1
访问量 31295
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
第12章 0224 - 数据库操作基础2,学习心得、笔记(mySql的,CURD操作, 预处理)
努力工作--周工--Robin
原创
843人浏览过

1. CURD操作

  1. -- 增加数据
  2. insert staffs(name,gender,salary,email,birthday) values
  3. ('test','male',99999,'test@php.cn','2021-1-1');
  4. -- 删除数据
  5. delete from staffs where salary = 99999;
  6. -- 修改数据
  7. update staffs set salary = salary + 1000 where salary < 5000;
  8. -- 条件查询
  9. select sid,name,salary from staffs where salary>9000;
  10. -- 条件查询,并按年龄从大到小排序
  11. select sid,name,birthday,salary from staffs where salary>9000
  12. order by timestampdiff(year, birthday, now()) desc;
  13. -- 模糊条件查询
  14. select sid,name from staffs where name like 'a%';
  15. select sid,name from staffs where name like '__m%';
  16. -- 分组查询
  17. select gender, count(*) num from staffs group by gender;
  18. -- 关联查询
  19. select aid,title,name from articles a, categories c where a.cid = c.cid;
  20. -- 所有国内新闻,改为c.cid=2, 为查所有国外新闻
  21. select aid,title,name from articles a, categories c where a.cid = c.cid and c.cid=1;
  22. -- 简化,哪果关联表中有同名字段,可以简化为如下
  23. select aid,title,name from articles natural join categories;

2. 预处理

  1. -- 生成预处理sql语句, ?号作占位符;
  2. prepare sqlStr from 'select sid,name,salary from staffs where salary > ? and salary < ? order by salary desc';
  3. -- 将真实的数据绑定到预处理语句的占位符上;
  4. set @sal1 = 6000, @sal2 = 10000;
  5. -- 执预处理sql语句
  6. execute sqlStr using @sal1, @sal2;
批改老师:天蓬老师天蓬老师

批改状态:合格

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

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

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