SQL 语句(一)

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: SQL 语句

DML 表查询

针对 表 中的 数据行 进行的增、删、改、查

增 insert into ...

-- 标准 
insert into 
st(id,name,age,gender,address,intime,cardnum,num)
values(1,'张三',18,'m','北京','2020-04-27','666666',10);
select * from st;
-- 部分列录入
insert into 
st(name,intime,num)
values('李四',NOW(),1111);
select * from st;

update 更新数据行的值

update st set name='张六'  where id=4;
select * from st;
update st set name='张qi' , age=21  where id=4;
select * from st;

delete 删除数据行

delete from st  where id=5;
select * from st;

伪删除:update 替代 delete ,添加状态列,1带表存在,0代表删除

添加状态列 
alter table st add column state tinyint not null default 1 comment '状态列,0是删除,1是存在';
使用update 替换 delete 
原: 
delete from st where id=4
修改后: 
update st set state=0 where id=4;

面试题 drop table t1 ,truncate table t1 , delete from t1 区别 ?

# drop table t1;    
  作用:1. 删除所有表数据,删除整个表段(rm ibd  ),属于物理性质,会释放磁盘空间。
       2. 删除表定义 (rm  frm , 元数据也会被删除)
# truncate table t1 ;
  作用:保留表结构,清空表段中的数据页。属于物理删除,会释放磁盘空间。
# delete from t1; 
  作用: 删除数据行。逐行删除。保留表结构,属于逻辑性质删除。只是标记删除,不会立即释放磁盘空间。
  所以delete 操作会产生碎片。

DQL 数据查询语句

select 独立使用(MySQL 独有)

-- 查询数据库服务器配置参数
select @@port;
select @@server_id;
select @@basedir;
select @@datadir;
select @@socket;
select @@innodb_flush_log_at_trx_commit;
-- 替代方法:
show variables;
show variables like '%trx%';

查询内置函数

select DATABASE();
select NOW();
select USER();
select CONCAT("hello world");
select user,host from mysql.user;
select CONCAT("数据库用户:",user,"@",host,plugin,";") from mysql.user;

其他数据库

select NOW() from dual;

select 通用使用方法

select 多子句执行顺序(单表)
select     列   
from       表  
where      条件  
group by   列 
having     条件 
order by   列 
limit      条件

生产中熟悉业务:

  1. comment
  2. desc ,简单查询表中数据,猜
  3. E-R关系图

select 配合 from 子句使用

查询表中所有数据(小表)
use world;
select id,name,countrycode ,district,population 
from city;
或者
select id,name,countrycode ,district,population 
from world.city;

select + from + where 子句使用

查询city表中,所有中国的城市信息。
select *  from city where countrycode = 'CHN';
查询人口数小于100人城市信息
select * from city where population<100;

where 配合逻辑连接符(and, or , between and),实现多条件过滤

查询人口数为100w-200w(包括两头)城市信息
select * from city where population >= 1000000 and population <= 2000000 
查询中国或美国的城市信息。
 select * from city where countrycode='CHN'  or countrycode='USA' ;
查询中国或美国,人口数大于500w的城市 
select * from world.city where countrycode in ('CHN','USA') and population >= 5000000;

select + from + group by + 聚合函数

--- 聚合函数:count()         
统计数量sum()           
求和avg()        
平均数max()          
最大值min()              
最小值group_concat()     
列转行


- group  by  分组功能原理
1. 按照分组条件进行排序
2. 进行分组列的去重复
3. 聚合函数将其他列的结果进行聚合。
--- SQL_MODE=only_full_group 
保证group by 语句 准确性
统计中国城市的个数
select COUNT(*) from city where countrycode='CHN';
统计中国的总人口数。
select SUM(population) from city where countrycode='CHN';
统计中国每个省的城市个数及城市名列表
mysql> select district, COUNT(name),name
    -> from city 
    -> where countrycode='CHN'  group by district 
    -> ;
ERROR 1055 (42000): Expression #3 of SELECT list is not in GROUP BY clause an

报错说明:5.7 之后的SQL对于group by语句的限制。

  1. 没有在group by 后
  2. 同时没有在函数中聚合操作违反了SQL_MODE=only_full_group_by
统计中国每个省的城市个数及城市名列表。
select district, COUNT(name),GROUP_CONCAT(name)
from city 
where countrycode='CHN'  group by district

having 子句应用

后判断,主要应用在group by之后需要判断。
--- 统计每个国家的总人口数,只显示总人口超过1亿人的信息
select countrycode,SUM(population)  
from city 
group by countrycode 
having  SUM(population)>100000000;

order by 语句应用

查询中国所有的城市信息,并按照人口数从大到小排序输出
select * from city where countrycode = 'CHN' order by population desc ;
每个国家的总人口数,总人口超过5000w的信息,并按总人口数从大到小排序输出
select countrycode,SUM(population)  
from city 
group by countrycode 
having  SUM(population)>50000000
order by SUM(population) desc ;

limit 分页查询

一般配合order by 使用

查询中国所有的城市信息,并按照人口数从大到小排序输出,只显示前十名。
select * 
from city 
where countrycode = 'CHN' 
order by population desc 
limit 10 ;
6-10 名 
select * 
from city 
where countrycode = 'CHN' 
order by population desc 
limit 5,5

送个福利

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
4天前
|
SQL 数据库
SQL语句大全
SQL语句大全
|
9月前
|
SQL
关于SQL语句,只有这么多了3
关于SQL语句,只有这么多了
|
9月前
|
存储 SQL 数据可视化
关于SQL语句,只有这么多了2
关于SQL语句,只有这么多了2
|
SQL 数据库
基本的sql语句
基本的sql语句
84 0
|
SQL 关系型数据库 MySQL
|
SQL 数据库 索引
常见SQL语句
常见SQL语句
122 0
|
SQL 关系型数据库 MySQL
一条 SQL 语句引发的思考
一条 SQL 语句引发的思考
一条 SQL 语句引发的思考
|
SQL 关系型数据库 MySQL
|
SQL 关系型数据库 文件存储