常用的sql语句(一)

版权声明:未经许可不得转载 https://blog.csdn.net/weixin_44141532/article/details/87867843

一、数据库相关操作

1.链接数据库

mysql -uroot -p  -- -u用户名 -p密码(不写密码时,不显示密码)

2.创建数据库

create database 数据库名 charset = utf8;  --"charset = utf8"表示使用utf8编码,支持中文

3.查看所有数据库

show databases;

4.使用数据库

use 数据库名;

5.查看当前使用的数据库

select database();

6.删除数据库

drop database 数据库名;

7.数据库的备份与恢复

-- 备份
mysqldump –uroot –p 数据库名 > python.sql; -- 重定向到文件中

-- 恢复
mysql -uroot –p 新数据库名 < python.sql; -- 导入的时候必须要先创建数据库

二、数据表的操作

1.创建表
create table 数据表名字 (字段 类型 约束);

create table classes(id int unsigned primary key auto_increment,name varchar(50) not null);

2.查看表的结构

desc 表名;

3.查看所有的表

show tables;

4.删除表

drop table 表名;

5.修改表

-- 添加字段
alter table 表名 add 字段 类型;

-- 修改字段 类型约束
alter table 表名 modify 字段 类型及约束;

-- 字段重命名
alter table 表名 change 原字段名 新字段名 类型约束;

-- 删除字段
alter table 表名 drop 字段;

三、数据的相关操作

1.全列插入

insert into 表名(字段...) values(...); -- 可以写部分字段

2.多行插入

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

3.删除数据

delete from 表名 where 条件;

4.修改

update 表名 set 字段=... where 条件;

5.查询

-- 查询所有列
select * from 表名;

--按一定条件查询
select * from 表名 where 条件;

--查询指定列
select1,列2 from 表名;

四、查询的相关操作

1.条件查询

  • 比较运算符
    = != <> > >= < <=
-- 查询学生表student中 age 不为18的学生信息
select * from student where age != 18;
select * from student where age <> 18;  --<>表示不等于

-- 查询学生表student中 age 大于等于18的学生姓名(字段name)
select name from student where age >= 18;
  • 逻辑运算符
    and 和
    or 或者
    not 不在
-- 查询18岁以上女生(字段sex)的信息
select * from student where age >18 and sex = "女"; 

-- 查询18岁以上 或者 性别为女的学生信息
select * from student where age>18 or sex = "女";

-- 查询不在18岁以上这个范围的学生信息
select * from student where not (age > 18 and sex = "女");
  • 模糊查询
    like
    %替换任意个 _替换一个
-- 查询有两个字的名字
select * from student where name like '__'

-- 查询至少有两个字的名字
select * from student where name like'__%'
  • 范围查询
    in 在一个非连续范围
    not in 不非连续范围
    between…and… 在一个连续的范围内
    not between … and… 不在连续范围内
-- 年龄为18或20岁学生信息
select * from student where age in(18,20);

-- 年龄不是18或20岁学生信息
select * from student where age not in(18,20);

-- 年龄在18到20岁之间学生信息
select * from student where age between 18 and 20;

-- 年龄不在18到20岁之间学生信息
select * from student where age not between 18 and 20;
  • 空判断
    is null 为空
    is not null 非空
-- 判断年龄为空的信息
select * from student where age is null;

-- 判断年龄不为空的信息
select * from student where age is not null;


2.排序
order by 排序
asc 从小到大
desc 从大到小

-- 查询年龄在12到20岁之间的女性,按照年龄从小到大到排序
	select * from students where age between 12 and 20 and sex = '女' order by age asc;

-- 查询年龄在12到20岁之间的女性,按照年龄从大到小排序
	select * from students where age between 12 and 20 and sex = '女' order by age desc;

3.聚合函数

  • 总数 count
  • 最大值 max
  • 最小值 min
  • 求和 sum
  • 平均值 avg
-- 查询女生人数
select count(*) from student where sex = '女';

-- 查询最大年龄
select max(age) from student;

-- 查询最小年龄
select min(age) from student;

-- 求年龄总和
select sum(age) from student;

--求年龄平均值
select avg(age) from student;


4.分组
group by 分组 having 分组条件

select 分组字段 from 表名 group by 分组字段 having 条件;  -- having 后常跟聚合函数

5.分页
limit start count;
limit 起始位置 显示的个数

-- 每页显示2个,第1个页面
select * from student limit 0,2;

-- 每页显示2个,第2个页面
select * from student limit 2,2;

-- 每页显示2个,第3个页面
select * from student limit 4,2;

猜你喜欢

转载自blog.csdn.net/weixin_44141532/article/details/87867843