1. CURD操作

  1. -- 增加数据
  2. insert staffs(name,gender,salary,email,birthday) values
  3. ('test','male',99999,'test@php.cn','2021-1-1');
  4. -- 删除数据
  5. delete from staffs where salary = 99999;
  6. -- 修改数据
  7. update staffs set salary = salary + 1000 where salary < 5000;
  8. -- 条件查询
  9. select sid,name,salary from staffs where salary>9000;
  10. -- 条件查询,并按年龄从大到小排序
  11. select sid,name,birthday,salary from staffs where salary>9000
  12. order by timestampdiff(year, birthday, now()) desc;
  13. -- 模糊条件查询
  14. select sid,name from staffs where name like 'a%';
  15. select sid,name from staffs where name like '__m%';
  16. -- 分组查询
  17. select gender, count(*) num from staffs group by gender;
  18. -- 关联查询
  19. select aid,title,name from articles a, categories c where a.cid = c.cid;
  20. -- 所有国内新闻,改为c.cid=2, 为查所有国外新闻
  21. select aid,title,name from articles a, categories c where a.cid = c.cid and c.cid=1;
  22. -- 简化,哪果关联表中有同名字段,可以简化为如下
  23. select aid,title,name from articles natural join categories;

2. 预处理

  1. -- 生成预处理sql语句, ?号作占位符;
  2. prepare sqlStr from 'select sid,name,salary from staffs where salary > ? and salary < ? order by salary desc';
  3. -- 将真实的数据绑定到预处理语句的占位符上;
  4. set @sal1 = 6000, @sal2 = 10000;
  5. -- 执预处理sql语句
  6. execute sqlStr using @sal1, @sal2;

更多相关文章

  1. SQL Server学习之路(一)
  2. 练习CURD常用操作,select查询与关联操作,预处理原理
  3. C# 数据操作系列 - 16 SqlSugar 完结篇
  4. C# 数据操作系列 - 13 SqlSugar 初探
  5. C# 数据操作系列 - 17 Dapper ——号称可以与ADO.NET 同台飙车的
  6. MySQL的EXPLAIN其实很简单
  7. CURD常用操作、select常用查询、预处理原理
  8. MySQL基础: 预处理和常用的MySQL函数
  9. mysql CURD常用操作及预处理机制

随机推荐

  1. 【20110115 InfoQ 百度技术沙龙 2010 第
  2. 【转】mysql-innodb-buffer pool 结构分
  3. C++编写数据库备份程序,支持MSSQL\Oracle
  4. 保持最新的一个字段值,直到它发生变化,然后
  5. mysql-proxy主从搭建读写分离全过程
  6. MyBatis排序时使用order by 动态参数时需
  7. [Step By Step]使用SAP Business Objects
  8. 如何在表单行SQL中检查是否有任何值为Nul
  9. 麻烦问下,为什么我的mysql存储过程一次调
  10. Ruby 1.9 + MySQL中发生访问错误