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;

更多相关文章

  1. mysql CURD常用操作及预处理机制
  2. 数据库CURD常用操作-select查询-预处理
  3. 慎用ToLower和ToUpper,小心把你的系统给拖垮了
  4. 如何校验内存数据的一致性,DynamicExpresso 算是帮上大忙了
  5. Chrome 85版重磅更新:网页加速10%,还有一波实用新功能上线!
  6. 使用 Redis 实现一个轻量级的搜索引擎,牛逼啊!
  7. 我就是不看好jpa
  8. 强大:MyBatis 流式查询
  9. grid对齐属性、grid实战php网站首页、媒体查询

随机推荐

  1. Android ListView(Selector 背景图片 全选
  2. Android碎片积累
  3. Android核心基础(五)
  4. 第12天android:短信发送+测试使用
  5. Android中NDK各版本下载
  6. Android(安卓)横竖屏幕切换
  7. Mac下获取android studio keystore的SHA1
  8. Android网络之HttpUrlConnection和Socket
  9. Android常用功能代码总结一
  10. android 隐藏ListView滚动条