博主信息
博文 47
粉丝 3
评论 0
访问量 50675
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
CURD常用操作、select常用查询、预处理原理
原创
1190人浏览过

1. CURD常用操作

  1. --
  2. insert users (name,gender,salary,email,birthday)
  3. values ('php','male',888,'php@php.cn','1993-08-03'),
  4. ('cn','male',888,'php@php.cn','1993-08-03');
  5. --
  6. delete from users where name = 'cn';
  7. -- users库中name值为php的改为helloworld
  8. update users set name = 'helloworld' where name = 'php';
  9. --
  10. select name,gender,email,salary from users where name = 'helloworld';

2. 常用select查询、关联查询

  • 2.1 常用select查询

  1. -- 条件查询:查询salary大于8000的用户
  2. select sid,name,salary from users where salary>8000;

  1. -- 区间查询:salary > 6000 salary <= 8000;
  2. select sid,name,salary from users where salary > 6000 and salary <= 8000;

  1. -- 分组查询:按性别分组查询,统计数量
  2. select gender 性别, count(*) 数量 from users group by gender;

  1. -- 排序查询:按年龄降序排序
  2. select sid,name,age,salary from users order by age desc limit 5;

  1. -- 分页查询:每页显示五条记录,查询第二页,偏移量公式:offset = (2-1) * 5 = 5
  2. select sid,name,email from users limit 5 offset 5;

  1. -- 集合查询:查询id353840
  2. select sid,name,salary from users where sid in (35,38,40);

  1. -- 模糊查询:查询名称第二个带o的用户
  2. select sid,name,salary from users where name like '_o%';

  1. -- 分组过滤查询:按性别分组查询,num统计数量,过滤条件为男性
  2. select gender, count(*) num from users group by gender having gender = 'female';
  • 2.2 关联查询
关联查询

左外连接

  1. -- 内连接使用join
  2. select a.aid,title,name
  3. from articles a join categories c
  4. using(cid);
  5. -- 关联查询
  6. select a.aid,title,name from articles a, categories c where a.cid = c.cid;
  7. -- 左外连接:左主表,右从表
  8. select *
  9. from articles a
  10. left join categories c
  11. on a.cid = c.cid;
  12. -- 右外连接:右主表,左从表
  13. select *
  14. from articles a
  15. right join categories c
  16. on a.cid = c.cid;

3. mysql预处理原理

  1. -- 生成预处理的sql语句
  2. prepare stmt from 'select sid,name,salary from users where salary > ? limit ?';
  3. -- 将真实的数据绑定到预处理语句的占位符
  4. set @salary=5000, @num=2;
  5. execute stmt using @salary, @num;
批改老师:天蓬老师天蓬老师

批改状态:合格

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

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

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