sql语法
1. 数据库表
选择数据库
user runoob;
设置使用的字符集
set names utf8:
读取数据表的信息
select * from table_name
注意
sql对于大小写不敏感.
sql的结束符为分号";".
sql中的字符串需要使用引号,字符串大小写敏感.
常见的sql命令
- select 查询(不会对数据进行操作)
- update 更新
- delete 删除
- insert into 插入
- create database 创建新数据库
- alter database 修改数据库
- drop table 删除表
- create index 创建索引
- drop index 删除索引
2.select语句
select语句用于从数据库中选取数据
结果被存储在一个结果集中,称为结果集
select column1,column2,....... from table_name;
- column: 选择的字段名称,可以为多个字段.选择*表示所有字段
- table_name:要查询的表名称
distinct语句
select distinct 语句用于返回唯一不同的值.
注意
位置只能出现在select后面
selecte distinct column1,column2,... from table_name;
where
用于过滤记录(提取满足条件的记录)
selecte column1,column2,... from table_name where condition;
where子句中的运算符
运算符 | 描述 |
---|---|
= | 等于 |
<> | 不等于 |
> | 大于 |
< | 小于 |
>= | 大于等于 |
<= | 小于等于 |
between | 在某个范围内 |
like | 模糊查询(搜索某种模式) |
in | 指定针对某个列的多个可能性 |
逻辑运算符
and 同时满足两个条件的值
select * from table_name where column1 > 1 and column2 > 2
or 满足其中一个条件的值
select * from table_name where column1 > 1 or column2 > 2
not 满足不包含该条件的值
select * from table_name where not column1 > 1
逻辑运算符的执行顺序
- ()
- not
- and
- or
特殊条件
is null 空值判断
select * from table_name where column is null;
between and :在某个范围之间
select * from table_name where column between 1000 and 1500;
查询该字段在1000~1500中,包含1000和1500.
范围:[]
in 在某个范围内
select * from table_name column in (1000,3000,5000);
查询的数据等于1000,3000,5000的值
like 模糊查询
select * from table_name where column like 'M%'
查询列中所有有m的值
- % :表示多个字符,_下划线表示一个字符
- M%:为能配符,正则表达式,表示的意思为模糊查询信息为 M 开头的。
- %M%:表示查询包含m的所有内容
- %M_:表示查询m在倒数第二位的所有内容
- _:表示一个占位符
拓展
where 子句并不一定带比较运算符,当不带运算符时,会执行一个隐式转换.0转换为false ,1表示true
select column from table_name where 0;
返回一个空集合,每一行记录where都返回false
select column from table_name where 1;
返回一个空集合,每一行记录where都返回true
and & or 运算符
AND & OR 运算符用于基于一个以上的条件对记录进行过滤。
如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。
如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。
结合and&or
select * from table_name
where column > 10
and (colum1 = 'a' or colum2 = 'b')
sql:查询出表中列大于10且列为a或者列为b
select * from table_name
where column > 10
and colum1 = 'a' or colum2 = 'b'
两者是不一样的.完全不一样
order by
用于对结果集进行排序
默认为升序,asc:升序;desc:降序
select * from table_name order by column1 ,column2 desc;
column1进行升序排序,column2进行降序排序
insert into
对于表进行插入新记录
-
无需指定要插入数据的列名,只需要提供被插入的值即可
insert into table_name values(value1,value2,value3.......)
-
需要指定列名及被插入的值
INSERT INTO table_name (column1,column2,column3,...)VALUES (value1,value2,value3,...);
nsert into select 和select into from 的区别
insert into scorebak select * from socre where aa='aa' --插入一行,要求表scorebak 必须存在
select * into scorebak from score where aa='aa' --也是插入一行,要求表scorebak 不存在
update
用于更新表中的记录(已经存在的)
update table_name
set column1 = value1,column2 = value2,...
where condition;
- condition:修改条件,用于指定哪些数据需要修改
注意
在使用update操作时,最好加上筛选条件
在 MySQL 中可以通过设置 sql_safe_updates 这个自带的参数来解决,当该参数开启的情况下,你必须在update 语句后携带 where 条件,否则就会报错。
set sql_safe_updates=1; 表示开启该参数
delete
用于删除表中的操作
delete from table_name
where condition;
删除表中的所有数据
在不删除表的情况下,删除表中所有的行。这意味着表结构、属性、索引将保持不变:
delete from table_name;
SELECT TOP, LIMIT, ROWNUM 子句
select top子句用于规定要返回的记录的数目(分页查询)
并非所有的数据库系统都支持 SELECT TOP 语句。 MySQL 支持 LIMIT 语句来选取指定的条数数据, Oracle 可以使用 ROWNUM 来选取.
mysql语法
SELECT column
FROM table_name
LIMIT number;
oracle语法
SELECT column
FROM table_name
WHERE ROWNUM <= number;
join 连接
SQL JOIN 子句用于把来自两个或多个表的行结合起来,基于这些表之间的共同字段。
SELECT column1, column2, ...
FROM table1
JOIN table2 ON condition;
不同的join
inner join : 如果表中有至少一个匹配,则返回行
left join:即使右表中没有匹配,也从左表中返回所有的行
right join 即使左表中没有匹配,也从右表中返回所有的行
full join:只要其中一个表中存在匹配,则返回行
INNER JOIN 关键字
在表中存在至少一个匹配时返回行。
select column
from table_name
inner join table_name2
on table_name.column = table_name.column;
或:
select column
from table_name
join table_name2
on table_name.column = table_name2.column;
inner join 与join一样
使用join时,on 和 where 条件的区别如下:
- on 条件是在生成临时表时使用的条件,它不管 on 中的条件是否为真,都会返回左边表中的记录
- where 条件是在临时表生成好后,再对临时表进行过滤的条件。这时已经没有 left join 的含义(必须返回左边表的记录)了,条件不为真的就全部过滤掉。
left join
LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。
SELECT column
FROM table1
LEFT JOIN table2
ON table1.column=table2.column;
或:
SELECT column
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name*;
right join
right JOIN 关键字从右表(table1)返回所有的行,即使左表(table2)中没有匹配。如果左表中没有匹配,则结果为 NULL。
SELECT column
FROM table1
right JOIN table2
ON table1.column=table2.column;
或:
SELECT column
FROM table1
right OUTER JOIN table2
ON table1.column_name=table2.column_name*;
FULL OUTER JOIN
FULL OUTER JOIN 关键字只要左表(table1)和右表(table2)其中一个表中存在匹配,则返回行.
FULL OUTER JOIN 关键字结合了 LEFT JOIN 和 RIGHT JOIN 的结果。
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
总结
A inner join B 取交集。
A left join B 取 A 全部,B 没有对应的值为 null。
A right join B 取 B 全部 A 没有对应的值为 null。
A full outer join B 取并集,彼此没有对应的值为 null。
对应条件在 on 后面填写。
UNION
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
请注意,UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同.
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL
UNION 操作符用于合并两个或多个 SELECT 语句的结果集。(会有重复数据)
where
SELECT column_name(s) FROM table1
where column_name = 'cn'
UNION ALL
SELECT column_name(s) FROM table2
where column_name = 'cn'
ORDER BY column_name;
SELECT INTO
可以从一个表复制信息到另一个表。
CREATE TABLE 新表
AS
SELECT * FROM 旧表
复制所有的列插入到新表中
SELECT *
INTO newtable [IN externaldb]
FROM table1;
复制希望的列插入到新表中
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
INSERT INTO SELECT
通过 SQL,您可以从一个表复制信息到另一个表。
INSERT INTO SELECT 语句从一个表复制数据,然后把数据插入到一个已存在的表中。目标表中任何已存在的行都不会受影响。
从一个表中复制所有的列插入到另一个已存在的表中
INSERT INTO table2
SELECT * FROM table1;
可以只复制指定的列插入到另一个已存在的表中
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;
总结
select into from 和 insert into select 都是用来复制表
两者的主要区别为: select into from 要求目标表不存在,因为在插入时会自动创建;insert into select from 要求目标表存在。
- 复制表结构及其数据:
create table table_name_new as select * from table_name_old
- 只复制表结构:
create table table_name_new as select * from table_name_old where 1=2;
或者:
create table table_name_new like table_name_old
- 只复制表数据:
如果两个表结构一样:
insert into table_name_new select * from table_name_old
如果两个表结构不一样:
insert into table_name_new(column1,column2...) select column1,column2... from