数据库常用sql
1.创建表
create table 表名(
字段名 类型 约束,
字段名 类型 约束
...
)
如:create table students(
id int unsigned primary key auto_increment,
name varchar(20),
age int unsigned,
height decimal(5,2)
)
2. 删除表
格式一:drop table 表名
格式二:drop table if exists 表名
3. 查询
select * from 表名
4. 添加数据
insert into 表名 values(...)
insert into 表名(字段1,...) values(值1,...)
格式一:insert into 表名 values(...),(...)...
格式二:insert into 表名(列1,...) values(值1,...),(值1,...)...
5.修改
update 表名 set 列1=值1,列2=值2... where 条件
6.删除
格式一:delete from 表名 where 条件
格式二:truncate table 表名(删除表的所有数据,保留表结构)
格式三:drop table 表名(删除表,所有数据和表结构都删掉)
Delete、Truncate、Drop的区别
1、Delete 删除数据时,即使删除所有数据,其中的自增长字段不会从1开始
2、Truncate 删除数据时,其中的自增长字段恢复从1开始
3、Drop 是删除表,所有数据和表结构都删掉
7. 起别名
select 别名.字段1,别名.字段2,... from 表名 as 别名
select s.name,s.sex,s.age from students as s;
8.去重
select distinct 字段1,... from 表名
例:查询所有学生的性别,不显示重复的数据
select distinct sex from students;
9.模糊查询
select * from students where name like '孙%'
10.范围查询
select * from students where hometown in('北京','上海','广东')
11.为空判断
select * from students where card is null
12.排序
select * from 表名 order by 列1 asc|desc,列2 asc|desc,...
asc从小到大排列,即升序
desc从大到小排序,即降序
select * from students order by age desc,studentNo
13.聚合函数
count(): 查询总记录数
max(): 查询最大值
min(): 查询最小值
sum(): 求和
avg(): 求平均值
14.分组
select 字段1,字段2,聚合... from 表名 group by 字段1,字段2...
15.获取部分行
select * from 表名 limit start,count
select * from students limit 0,3 (前三行)
16.分页查询
已知:每页显示m条数据,求:显示第n页的数据
select * from students limit (n-1)*m,m
17.连接查询
select * from 表1 inner join 表2 on 表1.列=表2.列
select * from 表1 left join 表2 on 表1.列=表2.列
select * from 表1 right join 表2 on 表1.列=表2.列
18.子查询
select * from students where age > (select avg(age) from students);
最新文章
- GitHub实战系列汇总篇
- video.js
- MyBatis学习总结(五)——实现关联表查询(转载)
- iOS 宏条件编译
- Android Sqlite基本命令
- MapReduce二次排序
- MyBatis学习--查询缓存
- 简单验证码识别(matlab)
- (function($){}) 和$(function(){}) 和$(function($){}) 区别
- JavaScript HTML DOM---遗漏知识再整理(向html添加/删除元素,改变内容和css)
- nginx配置解读
- Java字符编码
- linkin大话设计模式--简单工厂
- MySQL数据库学习三 数据库对象和基本操作
- 英语口语练习系列-C39-舞蹈-谈论昨天的活动
- spring-boot mybatis配置
- [PHP]PHP缓存机制之Output Control
- 使用Fidder从安卓模拟器获取APP内H5游戏网址
- squid搭建http/https代理服务器
- idea开发工具安装说明
热门文章
- ctfshow_web入门 xss
- 接口自动化库-apist
- Openfoam Pstream类探索
- JZOJ 2020.07.27【NOIP提高组】模拟
- CF1736C1 Good Subarrays (Easy Version)
- day06-动态SQL语句
- Epicor 助力F1车队Scuderia AlphaTauri 提升车队运营效率和性能
- 副三角形行列式转成上(下)三角形行列式为什么依次对换而不用第n行直接对换首行,第n-1行直接对换次行
- LCD1602液晶屏(续)
- pyinstaller打包Python程序报错OSError: Python library not found: libpython3.8.so, libpython3.8m.so