SQL基本语法整理

2021/8/19 2:06:05

本文主要是介绍SQL基本语法整理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

参考资料:

菜鸟教程 https://www.runoob.com/sql/sql-tutorial.html

 

 

-- 删除数据库
Drop database test20210816;

-- 新建数据库
create database db_name;

-- 新建表
create table users_table (
id int,
userName varchar(50),
phone varchar(30)
);

-- 添加1条数据,所有列都赋值
insert into users_table values (1008,"王五","13100000008","西藏");
-- 添加1条数据,指定列赋值
insert into users_table (id,phone) values (1001,"13123456789");

-- 添加n条数据 (存储过程)

-- 修改表结构,如添加列
alter table users_table add city varchar(10);

-- 更新数据
update users_table set id=1003, phone="13100000003" where userName is null;

-- 删除指定条件的数据
delete from users_table where id=1002;

-- 删除表格所有数据
delete from users_table;
-- 以下语句也是删除表格所有数据,但在workbench执行会提示语法错误
delete * from users_table;

-- 查询表数据
select * from users_table;

-- 查询返回唯一值
select distinct id, phone from users_table;

-- 查询条件and or运算符
select * from users_table where city="西藏" and id=1007;
select * from users_table where city="西藏" or id=1007;

-- 查询并排序 order by
select userName,phone from users_table order by userName asc;
select userName,phone from users_table order by phone desc;
select * from users_table order by userName; -- 默认升序
select * from users_table order by city,id; -- 先city升序,相同的再id升序

-- 查询并返回指定前n条数据。
select top 3 * from users_table; -- SQL Server语法
select top 50 percent * from users_table; -- SQL Server语法
select * from users_table limit 3; -- MySQL语法
select * from users_table where rownum <=3; -- Oracle语法

-- 查询 like操作符:包含
select * from users_table where userName like "用户%"; -- "%xx" "x%X" "%x%"
select * from users_table where userName not like "用户%";

-- 查询 通配符 % _ ^[xxx]
select * from users_table where userName like "用户%";
select * from users_table where city like "_京";
-- 第三种参考https://www.runoob.com/sql/sql-wildcards.html

-- 查询 in,一个字段匹配多值
select * from users_table where userName in ("张三","李四");

-- 查询 between,not between
select * from users_table where id between 1001 and 1005; -- 包括1001跟1005
select * from users_table where id not between 1001 and 1005;

-- 查询 as 别名
select userName as un, city as c from users_table; -- 列别名,结果表列名显示别名
select * from users_table as ut, score_table as st where ut.id = st.id; -- 表别名

-- inner join 内连接:返回两表条件相同的数据
select * from 表1 inner join 表2 on 表1.列=表2.列;
select * from 表1 join 表2 on 表1.列=表2.列;

-- left join 左连接:从左表(表1)返回所有的行,即使右表(表2)中没有匹配。如果右表中没有匹配,则结果为 NULL。
select * from 表1 left join 表2 on 表1.列=表2.列;
select * from 表1 left outer join 表2 on 表1.列=表2.列;

-- right join 右连接:从右表(表2)返回所有的行,即使左表(表1)中没有匹配。如果左表中没有匹配,则结果为 NULL。
select * from 表1 right join 表2 on 表1.列=表2.列;
select * from 表1 right outer join 表2 on 表1.列=表2.列;

-- full outer join ??连接:只要表1和表2其中一个表中存在匹配,则返回行。表1,2的并集。MySQL貌似不支持
select * from 表1 full outer join 表2 on 表1.列=表2.列;

-- cross join 交叉连接。笛卡尔积,表1 乘以 表2
select * from 表1 cross join 表2;

-- union 合并2个或以上的select结果,要求每个select结果之间的列数一样,列的数据类型相似
select 列 from 表1
union
select 列 from 表2;

select 列 from 表1
union all
select 列 from 表2;

 

未完待续。。。



这篇关于SQL基本语法整理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程