Blogger Information
Blog 47
fans 3
comment 0
visits 38227
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
CURD常用操作、select常用查询、预处理原理
Original
969 people have browsed it

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;
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:非常棒,要多写多看
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments