MySQL:CRUD

一.新增(Create)

全列插入:insert into 表名 values(值1,值2,...); (列与值顺序和数目对应)

指定列插入:insert into 表名(列名1,列名2) values(值1,值2);

一次性插入多条数据:insert into 表(列名1,列名2) values(值1,值2),(值1,值2),(值1,值2);

二.查询(Retrieve)

1.全列查询:select * from 表名;

2.指定列查询:select 列名,列名 from 表名;(一个或多个列名)

3.列为表达式的查询:select 列名/表达式 from 表名;

4.别名查询:select 列名 as 别名,列名 from 表名;

5.去重查询:select distinct 列名 from 表名;

6.排序:select 列名 from 表名 order by 列名 asc/desc;

7.条件查询:select 列名 from 表名 where 列名/表达式 比较/逻辑运算符 order by 列名 asc/desc;

例:select id,name as 姓名 from student where id = 1 order by id asc;

8.区间查询:select 列名 from 表名 where 列名 between 数值1 and 数值2;

select 列名 from 表名 where 列名>=数值1 and 列名<=数值2;

(前面这两条SQL语句都意为,取出某列数值在数值1,数值2的行数,记住是左闭右闭)

select 列名 from 表名 where 列名 in (数值1,数值2,数值3);

select 列名 from 表名 where 列名=数值1 or 列名=数值2 or 列名=数值3;

9.模糊查询:select 列名 from 表名 where 列名 like ...;

例1:select * from student where name ='孙%';(查出姓孙的所有同学,不管你孙后面是什么字符,不管后面有多少个字符)

例2:select * from student where name ='孙_'; (查出姓孙,且孙只有为一个字符的同学)

10.分页查询:select * from 表名 where 条件 order by 列名 asc|desc limit num; (默认下标为0)

select * from 表名 where 条件 order by 列名 asc|desc limit start,num;

select * from 表名 where 条件 order by 列名 asc|desc limit num offset start;

查询不为null的数据:select * from 表名 where 列名 is not null; (反之为查询数据为null的数据)

反例:select * from 表名 where 表名 <=> null;(是错误的!!查出的结果集为空)

补充:select null = null;(返回null)| select null <=> null; (返回true也就是1)

select null <> 1;(返回null)

记住:MySQL的null表示不定值,不表示空,任何值与不定值比较都为null。除<=>逻辑符号外,<=>对于null的比较是安全的,因此null<=>null返回1;

三.修改、更新(Update)

update 表名 set 列名=值 where 条件 order by 列名 asc|desc limit n;

(注意:如果不使用条件和limit约束,整表的数据都会被修改!语句逻辑:先确定表,再使用where条件筛选,接着order by 排序,把前n条数据进行修改。)(limit 在update语句中不能使用start)

四.删除(Delete)

delete from 表名 where 条件 order by 列名 asc|desc limit n;

(注意:注意:如果不使用条件和limit约束,整表的数据都会被删!)

相关推荐
这个DBA有点耶16 小时前
2026年分布式数据库有哪些主流选择?先评估这5个维度再决定
数据库·分布式·dba
这个DBA有点耶19 小时前
从MySQL 5.7到8.0:JSON查询性能差在哪?虚拟列索引vs多值索引怎么选
数据库·mysql·架构
胡写代码19 小时前
数据库审计字段,别再每张表各写一套了——我统一成这 6 个字段
数据库
SelectDB1 天前
为什么 JSON 正在成为分析数据库新的竞争点?
数据库·json·agent
ClouGence1 天前
开源数据库管理工具 CloudDM 4.2.0 发布,新增 GoldenDB、KingbaseES 等数据源
数据库·dba·devops
发霉的馒头1 天前
ORA-00845: MEMORY_TARGET not supported on this system的解决方法
数据库
草莓熊Lotso1 天前
【Redis 初阶】Set 类型深度解析:去重集合的运算能力与实战场景
linux·网络·数据库·windows·redis·tcp/ip·缓存
煎饼皮皮侠1 天前
【设计】设计一个web版的数据库管理平台后端(之五) --借鉴mybatis
数据库·mybatis
东风破_1 天前
danci 2:创建的单词书到底存在哪里?从 Supabase 一路理解 ORM、Drizzle 和 RLS
数据库·后端·node.js
九皇叔叔1 天前
《MySQL 体系架构详解:Server 层、SQL 执行流程与 InnoDB、MyISAM、MEMORY 存储引擎》
sql·mysql·架构