Blogger Information
Blog 41
fans 0
comment 0
visits 41101
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数据库增删改查(CURD)常用操作|常用的select查询|预处理
幸福敲门的博客
Original
1684 people have browsed it
  1. 熟练操作CURD中常用操作;
  2. 对于常用的select查询,必须全部达到背诵级别,特别是关联操作,一定要理解原理并上机实战;
  3. 明白预处理原理,并举例

一、数据库CURD中常用操作

数据库的增删改查

  1. -- 新增 张波和李文集
  2. insert staffs (name, gender, salary, email, birthday) values
  3. ('张波', 'male', 9500, 'zb@sinaw.com', '1999-12-30'),
  4. ('李文集', 'female', 8600, 'lwj@sinaw.com', '2000-01-01');
  5. -- 删除 张波
  6. delete from staffs where name = '张波';
  7. -- 修改 李文集为刘天好
  8. update staffs set name = '刘天好' where name = '李文集';
  9. -- 查询 刘天好
  10. select name, gender, salary, email, birthday from staffs where name = '刘天好';

二、select查询

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

图示:
查询salary大于9000的用户

  1. -- 区间查询:salary > 9000 salary <= 12000;
  2. select sid,name,salary from staffs where salary>=9000 and salary <= 12000;

图示:
区间查询

  1. -- 集合查询: in
  2. select sid,name from staffs where sid in (1,3,5);

图示:
集合查询

  1. -- like查询
  2. select sid,name from staffs where name like 'b%';
  3. select sid,name from staffs where name like '_a%';

like查询

  1. -- 分页查询
  2. -- limit 显示数量 offset 偏移量(跳过的记录数量)
  3. -- limit 偏移量, 显示数量
  4. -- limit 子句一定要放在select的最后
  5. -- 第一页: offset = ( 1 - 1 ) * 15 = 0
  6. select sid,name,email from staffs limit 15 offset 0;
  7. -- 第二页: offset = ( 2 - 1 ) * 15 = 15
  8. select sid,name,email from staffs limit 15 offset 15;
  9. -- 第三页: offset = ( 3 - 1 ) * 15 = 30
  10. select sid,name,email from staffs limit 15 offset 30;
  11. -- 第四页: offset = ( 4 - 1 ) * 5 = 15
  12. select sid,name,email from staffs limit 15 offset 45;

图示:
分页查询

  1. -- 排序
  2. select sid,name,age from staffs order by age asc;
  3. select sid,name,age from staffs order by age desc;
  4. select sid,name,age,salary from staffs order by age desc,salary asc;

asc正序排列

desc倒序排列

混合排列
混合排列

  1. -- 分组条件: having,不能用where
  2. select gender, count(*) num from staffs group by gender having gender = 'female';

分组条件having图示:
分组条件having

  1. -- 聚合函数: max(), min(), sum(), avg(), count()
  2. select gender, count(*) num from staffs group by gender;
  3. select gender, round(avg(age),2) avg_age from staffs group by gender;
  4. -- 分组条件: having,不能用where

聚合函数

2.3关联查询:

  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;

2.4-1创建视图:

  1. -- 创建视图
  2. create view v_staffs as select * from staffs;
  3. create view cw_staffs as select sid,name,salary from staffs;
  4. create view my_email as select '498668472@qq.com' as email;
  5. select * from staffs where email = (select email from my_email);

创建视图

2.4-2更新视图:

  1. -- 更新视图,会同步更新基本表
  2. update cw_staffs set salary = 8900 where sid = 2;
  3. drop view v_staffs;

更新视图图示:
更新视图

2.5

  1. -- 索引应该创建在经常被查询的字段,或者常出现在结果集中的字段上
  2. -- 索引分类: 普通索引,主键索引, 唯一索引, 全文索引
  3. create index 索引名称 on 表名(表字段)
  4. create index i_email on staffs (email);
  5. show index from staffs;
  6. create unique index unique_email on staffs (email);
  7. alter table test add primary key i_id (id);
  8. drop index i_email

show index from staffs图示:
show index from staffs

三、预处理原理

  • 防止 SQL 注入攻击
  • SQL 语句中的数据,只有在执行阶段再与字段进行绑定
  1. -- 生成预处理的sql语句
  2. prepare stmt from 'select sid,name,salary from staffs where salary > ? limit ?';
  3. -- 将真实的数据绑定到预处理语句中的占位符上 ?
  4. -- set @salary = 5000;
  5. -- set @num = 5;
  6. set @salary = 5000, @num = 5;
  7. 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
Author's latest blog post