SQL速查手册笔记

SQL 语句 语法
AND / OR SELECT column_name(s)
FROM table_name
WHERE condition
AND|OR condition
ALTER TABLE ALTER TABLE table_name
ADD column_name datatype

or

ALTER TABLE table_name
DROP COLUMN column_name

AS (alias) SELECT column_name AS column_alias
FROM table_name

or

SELECT column_name
FROM table_name AS table_alias

BETWEEN SELECT column_name(s)
FROM table_name
WHERE column_name
BETWEEN value1 AND value2
CREATE DATABASE CREATE DATABASE database_name
CREATE TABLE CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name2 data_type,
...
)
CREATE INDEX CREATE INDEX index_name
ON table_name (column_name)

or

CREATE UNIQUE INDEX index_name
ON table_name (column_name)

CREATE VIEW CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition
DELETE DELETE FROM table_name
WHERE some_column=some_value

or

DELETE FROM table_name
(Note: Deletes the entire table!!)

DELETE * FROM table_name
(Note: Deletes the entire table!!)

DROP DATABASE DROP DATABASE database_name
DROP INDEX DROP INDEX table_name.index_name (SQL Server)
DROP INDEX index_name ON table_name (MS Access)
DROP INDEX index_name (DB2/Oracle)
ALTER TABLE table_name
DROP INDEX index_name (MySQL)
DROP TABLE DROP TABLE table_name
GROUP BY SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value
IN SELECT column_name(s)
FROM table_name
WHERE column_name
IN (value1,value2,..)
INSERT INTO INSERT INTO table_name
VALUES (value1, value2, value3,....)

or

INSERT INTO table_name
(column1, column2, column3,...)
VALUES (value1, value2, value3,....)

INNER JOIN SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LEFT JOIN SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
RIGHT JOIN SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name
FULL JOIN SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
LIKE SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern
ORDER BY SELECT column_name(s)
FROM table_name
ORDER BY column_name [ASC|DESC]
SELECT SELECT column_name(s)
FROM table_name
SELECT * SELECT *
FROM table_name
SELECT DISTINCT SELECT DISTINCT column_name(s)
FROM table_name
SELECT INTO SELECT *
INTO new_table_name [IN externaldatabase]
FROM old_table_name

or

SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_table_name

SELECT TOP SELECT TOP number|percent column_name(s)
FROM table_name
TRUNCATE TABLE TRUNCATE TABLE table_name
UNION SELECT column_name(s) FROM table_name1
UNION
SELECT column_name(s) FROM table_name2
UNION ALL SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2
UPDATE UPDATE table_name
SET column1=value, column2=value,...
WHERE some_column=some_value
WHERE SELECT column_name(s)
FROM table_name
WHERE column_name operator value

打死也要记住法则1:col

打死也要记住法则2:select

SELECT col,col,col 找什么?

FROM table 从哪找?

WHERE col 条件 条件是啥?

条件:数字(where)

当查找条件col是数字

select * from table where col = 1;

Operator Condition SQL Example 解释
, !=, < <=, >, >= Standard numerical operators col != 4 等于 大于 小于
BETWEEN … AND … Number is within range of two values (inclusive) col BETWEEN 1.5 AND 10.5 在 X 和 X之间
NOT BETWEEN … AND … Number is not within range of two values (inclusive) co NOT BETWEEN 1 AND10 不在 X 和 X之间
IN (…) Number exists in a list col IN (2, 4, 6) 在 X 集合
NOT IN (…) Number does not exist in a list col NOT IN (1, 3, 5) 不在 X 集合

条件:文本(where)

当查找条件col是文本

select * from table where col like '%jin';

Operator Condition SQL Example 解释
= Case sensitive exact string comparison (notice the single equals) col = "abc" 等于
!= or <> Case sensitive exact string inequality comparison col != "abcd" 不等于
LIKE Case insensitive exact string comparison col LIKE "ABC" 等于
NOT LIKE Case insensitive exact string inequality comparison col NOT LIKE "ABCD" 不等于
% Used anywhere in a string to match a sequence of zero or more characters (only with LIKE or NOT LIKE) col LIKE "%AT%" (matches "AT", "ATTIC", "CAT" or even "BATS") 模糊匹配
_ Used anywhere in a string to match a single character (only with LIKE or NOT LIKE) col LIKE "AN_" (matches "AND", but not "AN") 模糊匹配单字符
IN (…) String exists in a list col IN ("A", "B", "C") 在集合
NOT IN (…) String does not exist in a list co NOT IN ("D", "E", "F") 不在集合

排序(rows)

需要对结果rows排序和筛选部分rows

select * from table where col > 1 order by col asc limit 2 offset 2

Operator Condition SQL Example 解释
ORDER BY . ORDER BY col ASC/DESC 按col排序
ASC . ORDER BY col ASC/DESC 升序
DESC . ORDER BY col ASC/DESC 降序
LIMIT OFFSET . LIMIT num_limit OFFSET num_offset 从offset取limit
ORDER BY . ORDER BY col1 ASC,col2 DESC 多列排序

join:连表(table)

当查找的数据在多张关联table里

select * from table1 left join table2 on table1.id = table2.id where col > 1

Operator Condition SQL Example 解释
JOIN .. ON .. . t1 JOIN t2 ON t1.id = t2.id 按ID连成1个表
INNER JOIN . t1 INNER JOIN t2 ON t1.id = t2.id 只保留id相等的row
LEFT JOIN . t1 LEFT JOIN t2 ON t1.id = t2.id 保留t1的所有row
RIGHT JOIN . t1 RIGHT JOIN t2 ON t1.id = t2.id 保留t2的所有row
IS/IS NOT NULL . col IS/IS NOT NULL col是不是为null

算式(select / where)

当需要对select的col 或 where条件的col 经过一定计算后才能使用

select *,col*2 from table where col/2 > 1

Operator Condition SQL Example 解释
+ - * / % . col1 + col2 col加减乘除
substr . substr(col,0,4) 字符串截取
AS . col * 2 AS col_new col取别名
...     还有很多

统计(select)

对查找的rows需要按col分组统计的情况

select count(*),avg(col),col from table where col > 1 group by col

Operator Condition SQL Example 解释
COUNT(*), COUNT(column) A common function used to counts the number of rows in the group if no column name is specified. Otherwise, count the number of rows in the group with non-NULL values in the specified column. count(col) 计数
MIN(column) Finds the smallest numerical value in the specified column for all rows in the group. min(col) 最小
MAX(column) Finds the largest numerical value in the specified column for all rows in the group. max(col) 最大
AVG(column) Finds the average numerical value in the specified column for all rows in the group. avg(col) 平均
SUM(column) Finds the sum of all numerical values in the specified column for the rows in the group. sum(col) 求和
GROUP BY . group by col,col2 分组
HAVING . HAVING col>100 分组后条件

子表 (table)

一次select的结果rows作为下一次select的临时table才能得到最终结果

select * from (select * from table where col > 1) as tmp where col < 1

Operator Condition SQL Example 解释
(select -)as tmp   (select -)as tmp select结果做子表
in(select -)   in(select -) select结果做条件
avg(select -)   avg(select -) select结果做条件

来源http://xuesql.cn/static/%E9%87%91%E8%80%81%E5%B8%88%E6%89%8B%E5%86%8C.html

发布了287 篇原创文章 · 获赞 297 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_41895747/article/details/104115945