sql语句

基础的查询

展示所有数据库

show databases;

删除数据库

drop database 数据库名;
创建数据库
create database 数据库名 charset utf8;

进入数据库

use 数据库名;
展示所有的表
show tables;
“”" 查看表结构 “”"
desc 表名;
“”" 修改表名 “”"
alter table 原表名 rename 新表名;
“”" 删除表 “”"
drop table 表名;
“”" 创建表 “”"
create table 表名(
字段名1 类型 [约束],
字段名2 类型 [约束]
查询数据 “”"
select * from 表名;
给表中添加数据 “”"
insert into 表名(字段1,字段2,字段3)
values(数据1,数据2,数据3);
增加字段 “”"
alter table 表名 add 字段名 类型;
删除字段 “”"
alter table 表名 drop 字段名;
修改字段名 “”"
alter table 表名 change 原字段名 新字段名 类型[约束];
修改字段类型 “”"
alter table 表名 modify 字段名 类型[约束];
修改所有数据 “”"
updata 表名 set 字段名=新值;
修改一条数据 “”"
update 表名 set 字段名=新值 where 条件;
案例:update people set ssex=“男” where sname =“光头强”;
“”" 删除所有数据 “”"
delete from 表名;
“”" 删除一条数据 “”"
delete from 表名 where 条件;
案例:delete from people where sname=“玛卡巴卡”;

时间

date:年月日
time:时分秒
datetime:年月日时分秒
decimal 小数

枚举

enum:单选 enum(“男”,“女”,“中性”)
集合 “”"
set:多选 set(“游泳”,“下棋”,“唱歌”)

约束

非空约束
not null
默认约束 “”"
default
唯一约束 “”"
unique
主键约束 “”"
primary key
自增约束 “”"
auto_increment
比较运算符 “”"
查询指定的数据 “”"
= < > <= >= <>或!=(这两个意思相同)
select * from 表名 where 条件;
select * from 表名 where id<=4;

逻辑运算符

指定条件查询

and 两个条件同时满足
or 两个条件满足一个就好
select 字段名 from 表名 where 数据1 and(or) 数据2
select * from 表名 where id > 3 or id<2;

模糊查询

like
% 表示任意多个任意字符
_ 表示一个任意字符

查询姓名中以小开头的信息

select * from 表名 where 字段名 like ‘李%’;

查询名字中有"小"的名字

select * from stu where name like “%小%”;

查询以香结尾的数据

select * from stu where name like “%香”

查询至少有两个字的名字

select * from stu where name like “__%”;

范围查

in 表示在一个非连续的范围内
select * from 表名 where 字段名 in(1,3,5);
between … and … 和 >= and <= 性质相同

表示在一个连续的范围内

select * from 表名 where 字段名 between 数据1 and 数据2;(在数据1和数据2之间的数)

空判断

判空: is null

查询没有填写身高的学生

select * from 表名 where 字段名 is null;
判非空:is not null

查询填写了身高的学生

select * from 表名 where 字段名 is not null;

max() 最大

select max(age) from stu;

min() 最小

select min(age) from stu;

count() 总数

select count(*) from stu;

sum() 总和

select sum(age) from stu;

avg() 平均值

select avg(age) from stu;

round(小数,保留位数)

select round(avg(age),2) from stu;

以性别进行分组获取性别

group by 分组
select 字段名1 from 表名 group by 字段名1;
select gender as “性别”, group_concat(name) as “学生” from students group by gender;

过滤

having:过滤,对分组后的结果进行筛选 分组在前 过滤在后

查询每种性别的平均年龄

select gender,avg(age) from stu group by gender;

平均年龄超过30打印出来

select gender,avg(age),group_concat(name) from stu group by gender having avg(age)>30;

外键约束

foreign key (外键列) references 主键表(主键列);
foreign key(uid) references a(id);

添加外键

alter table 表名 add foreign key(字段名) references 表名(字段名);
alter table b add foreign key(a_id) references a(id);

查看外键

show create table 有外键的表名;
alter table 表名 drop foreign key 外键字段名;
alter table b drop foreign key b_ibfk_1;

内连接

伪代码:select * from 表1 inner join 表2 on 表1.字段名1=表2.字段名2; # 显示内连接
案例:select * from provinces inner join users on provinces.id=users.uid;

外连接

select * from 表名1 left/right join 表名2 on 表名1.字段名1=表名2.字段名2;
select * from provinces right/left users on provinces.id=users.uid;

子查询语句

查询所有有学生的班级 要求显示班级名称和学生的姓名

select * from student where cls_id in (select id from class);
查找班级年龄最大,身高最高的学生
select* from student where cj=(select max(cj) from student) and height=(select max(height) from student);
或 select * from student where (cj,height) = (select max(cj),max(height) from student);
需要给子查询语句增加一个any条件

查询出 年龄 比 (身高大于175的人的年龄) 中的任意一个大的都可以

select * from stu where age>any(select age from stu where height>175);

查询身高大于 (年龄小于30岁的身高) 中所有的身高都大才行

select * from stu where height>all(select height from stu where age<30);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值