PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > CURD常用操作、select常用查询、预处理原理

CURD常用操作、select常用查询、预处理原理

吳
原创
2021年02月25日 18:46:01 937浏览

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;
上一条: 重载get 下一条: 1216CSS优先级
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议