第12章 0224 - 数据库操作基础2,学习心得、笔记(mySql的,CURD操作, 预处理)
16lz
2021-03-02
1. CURD操作
-- 增加数据
insert staffs(name,gender,salary,email,birthday) values
('test','male',99999,'test@php.cn','2021-1-1');
-- 删除数据
delete from staffs where salary = 99999;
-- 修改数据
update staffs set salary = salary + 1000 where salary < 5000;
-- 条件查询
select sid,name,salary from staffs where salary>9000;
-- 条件查询,并按年龄从大到小排序
select sid,name,birthday,salary from staffs where salary>9000
order by timestampdiff(year, birthday, now()) desc;
-- 模糊条件查询
select sid,name from staffs where name like 'a%';
select sid,name from staffs where name like '__m%';
-- 分组查询
select gender, count(*) num from staffs group by gender;
-- 关联查询
select aid,title,name from articles a, categories c where a.cid = c.cid;
-- 所有国内新闻,改为c.cid=2, 为查所有国外新闻
select aid,title,name from articles a, categories c where a.cid = c.cid and c.cid=1;
-- 简化,哪果关联表中有同名字段,可以简化为如下
select aid,title,name from articles natural join categories;
2. 预处理
-- 生成预处理sql语句, 用 ?号作占位符;
prepare sqlStr from 'select sid,name,salary from staffs where salary > ? and salary < ? order by salary desc';
-- 将真实的数据绑定到预处理语句的占位符上;
set @sal1 = 6000, @sal2 = 10000;
-- 执预处理sql语句
execute sqlStr using @sal1, @sal2;
更多相关文章
- SQL Server学习之路(一)
- 练习CURD常用操作,select查询与关联操作,预处理原理
- C# 数据操作系列 - 16 SqlSugar 完结篇
- C# 数据操作系列 - 13 SqlSugar 初探
- C# 数据操作系列 - 17 Dapper ——号称可以与ADO.NET 同台飙车的
- MySQL的EXPLAIN其实很简单
- CURD常用操作、select常用查询、预处理原理
- MySQL基础: 预处理和常用的MySQL函数
- mysql CURD常用操作及预处理机制