后端MySQL常用命令

不是专业后端的,所有可能有些不太准确,都是自己平时在项目当中总结的,谢谢ฅฅ*

添加/insert

给指定字段添加数据

INSERT INTO 表名 字段名 VALUES 值

sql 复制代码
INSERT INTO ninth_student (id,name,sex,age,weight,birth) VALUES (0,'亚亚','女',18,47,'2002-05-23')

给全部字段添加数据

INSERT INTO 表名 VALUES 值

批量添加数据

INSERT INTO 表名 字段名 VALUES 值1,值2

INSERT INTO 表名 VALUES 值1,值2

sql 复制代码
INSERT INTO ninth_student VALUES (0,'哎亚亚','男',20,50,'2003-04-16'),(0,'SUN亚蛋','女',21,47,'2002-09-08'),(0,'哈哈哈','男',25,80,'1998-02-01'),(0,'嘎嘎嘎','男',30,75,'1993-05-12'),(0,'噜啦啦','女',23,52,'2000-11-28')

修改/update

UPDATE 表名 set 字段名=值 WHERE 条件

sql 复制代码
UPDATE ninth_student set birth='2005-04-07' WHERE id=1

一定要加where条件,要不然会修改整张表的所有数据

删除/delete

DELETE FROM 表名 WHERE 条件

sql 复制代码
DELETE FROM ninth_student WHERE id=8

一定要加where条件,要不然会删除整张表的所有数据

查询/select

基本查询

SELECT 字段名 from 表名

sql 复制代码
SELECT id,name,sex,age,weight,birth from ninth_student

条件查询

用where表示查询的条件

SELECT 字段名 from 表名 WHERE 条件

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student WHERE id BETWEEN 1 and 5

SELECT 字段名 from 表名 WHERE 条件 and/or 条件

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student WHERE id>5 and sex='男'
SELECT id,name,age,sex,weight,birth from ninth_student WHERE id>5 OR sex='男'

模糊查询/like

利用%

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student WHERE name LIKE '%亚'
SELECT id,name,age,sex,weight,birth from ninth_student WHERE name LIKE '亚%'
SELECT id,name,age,sex,weight,birth from ninth_student WHERE name LIKE '%亚%'

利用_,一个_代表一个字符,如果前面有两个字,就加两个

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student WHERE name LIKE '_马'

分页查询/limit

SELECT 字段名 表名 LIMIT (n-1)*每页展示个数,每页展示个数

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student LIMIT 0,4
SELECT id,name,age,sex,weight,birth from ninth_student LIMIT 4,4

排序

SELECT 字段名 from 表名 ORDER BY age ASC/DESC

sql 复制代码
SELECT id,name,age,sex,weight,birth from ninth_student ORDER BY age ASC
SELECT id,name,age,sex,weight,birth from ninth_student ORDER BY id DESC

聚合函数

SELECT COUNT(0) from 表名:表示总共有几条数据

SELECT SUM(字段名) from 表名:求和

SELECT AVG(字段名) from 表名:平均数

SELECT MIN(字段名) from 表名:最大

SELECT MAX(字段名) from 表名:最小

SELECT CAST(AVG(字段名) as DECIMAL(10,0)) as AVG from 表名:类型转换

sql 复制代码
SELECT COUNT(0) from ninth_student
SELECT SUM(age) from ninth_student
SELECT AVG(age) from ninth_student
SELECT MIN(age) from ninth_student
SELECT MAX(age) from ninth_student
SELECT CAST(AVG(age) as DECIMAL(10,0)) as AVG from ninth_student

分组查询

SELECT 字段名,聚合函数 from 表名 GROUP BY 字段名

GROUP BY 字段名:根据...分组

group by 分组字段,select后跟的也是相应的分组字段,他俩是一致的

where是分组前,having是分组后

having后面跟条件的话,也只能跟聚合函数

sql 复制代码
SELECT sex,COUNT(0),AVG(age) from ninth_student GROUP BY sex
SELECT sex,COUNT(0),AVG(age) from ninth_student WHERE age>22 GROUP BY sex HAVING AVG(age)>20

子查询

把查询到的结果当做另一个查询的条件

SELECT 字段名 from 表1 WHERE 字段名1 = (SELECT 表1_字段名1 from 表 WHERE 字段名='值')

sql 复制代码
SELECT name from dept WHERE id = (SELECT dept_id from ninth_student WHERE name='小马')

内连接

多表联查,一定要待条件!!!

inner join 表示内连接

on 表和表之间的连接,只能用on

inner join ... on ... 只要看见join就加on

看到,就写where

SELECT 表1.字段名1,表1.字段名1,字段名1,字段名1,字段名1,字段名1,表2.字段名2,表2.字段名2 from 表1 INNER JOIN 表2 ON 表1.字段名1 = 表2.字段名2

sql 复制代码
SELECT ninth_student.id,ninth_student.name,age,sex,weight,birth,dept.id,dept.name from ninth_student INNER JOIN dept ON ninth_student.id = dept.id
SELECT ninth_student.id,ninth_student.name,age,sex,weight,birth,dept.id,dept.name from ninth_student,dept WHERE ninth_student.dept_id = dept.id
SELECT ninth_student.id,ninth_student.name,age,sex,weight,birth,dept.id,dept.name from ninth_student,dept WHERE ninth_student.id = dept.id

外连接

在实际开发中,左外连接用的比较多

left join查询左边表的所有部分和右边表的交集,就是以左边的表为主

左外是以左边的表为主表,查询的结果只关注左表,不在乎右边

right join查询的是右边表的所有部分和左边表的交集,就是以右边的表为主

sql 复制代码
SELECT ninth_student.id,ninth_student.name,sex,age,weight,birth,dept.id,dept.name from dept LEFT JOIN ninth_student ON ninth_student.dept_id = dept.id
SELECT ninth_student.id,ninth_student.name,sex,age,weight,birth,dept.id,dept.name from dept RIGHT JOIN ninth_student ON ninth_student.dept_id = dept.id
相关推荐
小光学长1 小时前
基于vue框架的防疫科普网站0838x(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库
极限实验室1 小时前
使用 Docker Compose 简化 INFINI Console 与 Easysearch 环境搭建
数据库·docker·devops
秋千码途1 小时前
小架构step系列08:logback.xml的配置
xml·java·logback
飞翔的佩奇1 小时前
Java项目:基于SSM框架实现的旅游协会管理系统【ssm+B/S架构+源码+数据库+毕业论文】
java·数据库·mysql·毕业设计·ssm·旅游·jsp
时来天地皆同力.2 小时前
Java面试基础:概念
java·开发语言·jvm
智海观潮2 小时前
Flink CDC支持Oracle RAC架构CDB+PDB模式的实时数据同步吗,可以上生产环境吗
大数据·oracle·flink·flink cdc·数据同步
找不到、了2 小时前
Spring的Bean原型模式下的使用
java·spring·原型模式
企企通采购云平台2 小时前
「天元宠物」×企企通,加速数智化升级,“链”接萌宠消费新蓝海
大数据·人工智能·宠物
Apache Flink2 小时前
Flink Forward Asia 2025 主旨演讲精彩回顾
大数据·flink
阿华的代码王国2 小时前
【Android】搭配安卓环境及设备连接
android·java