简单的SQL语句

最近在学习SQL语句,为了我不常常遗忘,于是把一些基本的语法记录下来,方便我以后查询。

1、切换当前的数据库

USE student

切换到名为student的数据库。

2、创建表格

CREATE TABLE student_table(name varchar(10) not null PRIMARY KEY,number int null,chinese int null)

创建一个名为student_table的表格,其中name为主键。

3、向表格中插入数据:
一次插入一行数据:
INSERT INTO student_table
VALUES('wang',1,90)
向表格student_table中插入一行数据
插入多行数据;
insert  table (’字段',’字段',’字段',’字段',...)
select 值,值,值,值....
union
select 值,值,值,值....
union
select 值,值,值,值....
使用union的字段也可以使用union all,区别在于:
使用union:若插入的数据相同,则只会保留一行(会合并)
使用union all:若插入相同数据时,会保留多行(不会合并)
4、查询列:
SELECT name FROM student_table


从student_table中查询名为name的列
5、查询所有列
SELECT * FROM student_table
从student_table中查询所有列

5、删除表格
DROP TABLE student_table
6、更改列标题显示:
select name as '姓名' from student_table
表中为name的列,现在显示为“姓名”了
7、有条件的选择查询:
select id from student_table
where id>=5
从student_table表中查询id大于等于5的选项
8、SQL语言中的逻辑运算符:AND、OR、NOT
eg:
select id from student_table
where id>3 and id<=6

也可以使用between来下定数据的范围:
eg:
select id from student_table
where id between 3 and 5
效果和上面使用逻辑运算符and相同。

使用in和or的效果相同:
select id from student_table
where id in(4,5,6)

select id from student_table
where id=4 or id=5 or id=6

9、空值判断
select id from student_table
where id is not null
10、模糊查询;
格式:列名 [not] like 通配符
通配符%:代表任意多个字符
通配符_:代表任意一个字符
通配符[]:括号中列出的任意一个字符
通配符[^]:括号中列出以外的任意一个字符
eg:
select name from student_table
where name like '刘%'
查询姓刘的人
select name from student_table
where name like '刘_ _'
查询名为刘某某的人
select name from student_table 
where name like '[刘 王]%'
查询姓王或姓刘的人

select name from student_table 
where name like '[^刘 王]%'


查询既不姓名也不姓刘的人
10、分组汇总:GROUP BY 列表名
分组筛选:HAVING 逻辑表达式
排序查询结果:ORDER BY 列名表达式表 ASC|DESC
11、连接查询:
内连接:from 表名1 inner join 表名2 on 连接条件
外连接:
左外连接:对左边的表不加限制 from 表名1 left join 表名2 on 连接表达式
右外连接:对右边的表不加限制 from 表名1 right join 表名2 on 连接表达式
全外连接:同时实现左右连接 from 表名1 full join 表名2 on 连接表达式
未完待续。。。。。。。。。。。








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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值