飞道的博客

建议收藏!!十分钟带你从零学会最最最常用的SQL语句

443人阅读  评论(0)


1. 前言

很多时候最简单的东西反而是最容易忘的,特别是当我们在学习新的知识的时候,面对老师总结的密密麻麻的知识点,我们总是无能为力。不得不承认,详细的知识点包含常用和不常用的知识,很全面,这是好的,但作为我们随拿随用的知识总结,则需要最精华,最常用,最通俗易懂的知识。因此我总结了以下SQL语句。

2. 数据库和表的操作

2.1. 连接MySQL:

mysql -h主机地址 -P端口 -u用户名 -p密码

2.2. 查看数据库:

show databases;

2.3. 使用数据库:

use 数据库名;

2.4. 查看表:

show tables;

2.5. 建立表:

create table 表名称(字段名1 int not null primary key, 字段名2 char(20) not null);

2.6. 删除表:

drop table 表名称;

3. 表内容的操作(增删查改)

3.1. 插入:

insert into 表名称 values(字段名1,值1),(字段名2,值2);

3.2. 条件查询:

select * from 表名称 where 字段名 < 20;

3.3. 全查询:

select * from 表名称;

3.4. 多表条件查询

select 表名称1.字段名1,表名称1.字段名2,表名称2.字段名3 from 表名称1,表名称2 where 表名称1.字段名1=表名称2.字段名3;

3.5. 删除:

delete from 表名称 where 字段名 = 4;

3.6. 更新:

update 表名称 set 字段名1 =值1 where 字段名2=值2 ;

4. 函数

求和        sum(字段名)
求平均值     avg(字段名)
计数        count(字段名)
求最大值     max(字段名)
求最小值     min(字段名)

这是一个例子:

select sum(字段名) from 表名称;

5. 修饰符

limit 限制查询结果数量

select * from 表名称 limit 5;

order by 排序(默认是升序)

select * from 表名称 order by 字段名;

desc 降序

select * from 表名称 order by 字段名 desc;

asc 升序

select * from 表名称 order by 字段名 asc;

group by 分组

select 字段名1 from 表名称 group by 字段名2;

转载:https://blog.csdn.net/PaperJack/article/details/117305837
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场