0%

SQL 语法一览

资源


你想说的话, 都是你想表达的意思

SELECT [DISTINCT] [table.]col[, …[table.]col] // 查哪些列 (DISTINCT 去重)
FROM table0 ([…(INNER JOIN | (RIGHT | LEFT)OUTER JOIN) table1 ON table0.main_key = table1.main_key] | [, …table]) // 从哪些表
WHERE (condition […( AND | OR) (condition | sub query)] | [table.]col [NOT] IN (sub query | refer)) // 有哪些条件
GROUP BY [DISTINCT] [table.]col[, …[table.]col] // 如何分组
HAVING (condition […( *AND | OR ) (condition | sub query)] | [table.]col [NOT] IN (sub query | refer)) // 分组后如何过滤
ORDER BY [table.]col[, …[table.]col] [(ASC | DESC)] // 如何排序
LIMIT int OFFSET int // 最多返回多少条, 从哪条开始(默认0)
UNION // 是否组合其他查询(通过 UNION 组合的每个查询必须包含相同的列、表达式或聚集函数)


操作符号

AND

1
2
3
SELECT *
FROM table
WHERE condition AND condition

OR

1
2
3
SELECT *
FROM table
WHERE condition OR condition

IN

1
2
3
SELECT *
FROM table
WHERE col IN (refer ,refer)

WHERE col = 1 OR col = 2
等价于
WHERE col IN (1 ,2)

NOT

1
2
3
SELECT *
FROM table
WHERE col NOT IN (refer ,refer)

WHERE col != 1 OR col != 2
等价于
WHERE col NOT IN (1 ,2)


数据库

查看数据库

1
SHOW databases;

创建数据库

创建一个名为 db_name, 编码为 utf-8(真·utf-8)的数据库

1
CREATE DATABASE db_name character set utf8mb4;

删除数据库

1
DROP DATABASE db_name;

选择(使用)数据库

1
USE db_name;