title | date | order | categories | tags | permalink | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
SQL Cheat Sheet |
2022-07-16 07:17:08 -0700 |
99 |
|
|
/pages/e438a7/ |
SELECT
*FROM
table_name;
SELECT DISTINCT
column_name;
SELECT
column1, column2FROM
table_nameWHERE
condition;SELECT
*FROM
table_nameWHERE
condition1AND
condition2;SELECT
*FROM
table_nameWHERE
condition1OR
condition2;SELECT
*FROM
table_nameWHERE NOT
condition;SELECT
*FROM
table_nameWHERE
condition1AND
(condition2OR
condition3);SELECT
*FROM
table_nameWHERE EXISTS
(SELECT
column_nameFROM
table_nameWHERE
condition);
SELECT
*FROM
table_nameORDER BY
column;SELECT
*FROM
table_nameORDER BY
columnDESC
;SELECT
*FROM
table_nameORDER BY
column1ASC
, column2DESC
;
SELECT TOP
number columns_namesFROM
table_nameWHERE
condition;SELECT TOP
percent columns_namesFROM
table_nameWHERE
condition;- 并非所有数据库系统都支持
SELECT TOP
。 MySQL 中是LIMIT
子句 SELECT
column_namesFROM
table_nameLIMIT
offset, count;
- % (percent sign) 是一个表示零个,一个或多个字符的通配符
- _ (underscore) 是一个表示单个字符通配符
SELECT
column_namesFROM
table_nameWHERE
column_nameLIKE
pattern;LIKE
‘a%’ (查找任何以“a”开头的值)LIKE
‘%a’ (查找任何以“a”结尾的值)LIKE
‘%or%’ (查找任何包含“or”的值)LIKE
‘_r%’ (查找任何第二位是“r”的值)LIKE
‘a*%*%’ (查找任何以“a”开头且长度至少为 3 的值)LIKE
‘[a-c]%’(查找任何以“a”或“b”或“c”开头的值)
- 本质上,IN 运算符是多个 OR 条件的简写
SELECT
column_namesFROM
table_nameWHERE
column_nameIN
(value1, value2, …);SELECT
column_namesFROM
table_nameWHERE
column_nameIN
(SELECT STATEMENT
);
SELECT
column_namesFROM
table_nameWHERE
column_nameBETWEEN
value1AND
value2;SELECT
*FROM
ProductsWHERE
(column_nameBETWEEN
value1AND
value2)AND NOT
column_name2IN
(value3, value4);SELECT
*FROM
ProductsWHERE
column_nameBETWEEN
#01/07/1999# AND #03/12/1999#;
SELECT
*FROM
table_nameWHERE
column_nameIS NULL
;SELECT
*FROM
table_nameWHERE
column_nameIS NOT NULL
;
SELECT
column_nameAS
alias_nameFROM
table_name;SELECT
column_nameFROM
table_nameAS
alias_name;SELECT
column_nameAS
alias_name1, column_name2AS
alias_name2;SELECT
column_name1, column_name2 + ‘, ‘ + column_name3AS
alias_name;
- 每个 SELECT 语句必须拥有相同的列数
- 列必须拥有相似的数据类型
- 每个 SELECT 语句中的列也必须具有相同的顺序
SELECT
columns_namesFROM
table1UNION SELECT
column_nameFROM
table2;UNION
仅允许选择不同的值,UNION ALL
允许重复
ANY
如果任何子查询值满足条件,则返回 true。ALL
如果所有子查询值都满足条件,则返回 true。SELECT
columns_namesFROM
table1WHERE
column_name operator (ANY
|ALL
) (SELECT
column_nameFROM
table_nameWHERE
condition);
SELECT
column_name1, COUNT(column_name2)FROM
table_nameWHERE
conditionGROUP BY
column_name1ORDER BY
COUNT(column_name2) DESC;
SELECT
COUNT
(column_name1), column_name2FROM
tableGROUP BY
column_name2HAVING
COUNT(
column_name1)
> 5;
INSERT INTO
table_name (column1, column2)VALUES
(value1, value2);INSERT INTO
table_nameVALUES
(value1, value2 …);
UPDATE
table_nameSET
column1 = value1, column2 = value2WHERE
condition;UPDATE
table_nameSET
column_name = value;
DELETE FROM
table_nameWHERE
condition;DELETE
*FROM
table_name;
SELECT COUNT (DISTINCT
column_name)
;
SELECT MIN (
column_names) FROM
table_nameWHERE
condition;SELECT MAX (
column_names) FROM
table_nameWHERE
condition;
SELECT AVG (
column_name) FROM
table_nameWHERE
condition;
SELECT SUM (
column_name) FROM
table_nameWHERE
condition;
SELECT
column_namesFROM
table1INNER JOIN
table2ON
table1.column_name=table2.column_name;SELECT
table1.column_name1, table2.column_name2, table3.column_name3FROM
((table1INNER JOIN
table2ON
relationship)INNER JOIN
table3ON
relationship);
SELECT
column_namesFROM
table1LEFT JOIN
table2ON
table1.column_name=table2.column_name;
SELECT
column_namesFROM
table1RIGHT JOIN
table2ON
table1.column_name=table2.column_name;
SELECT
column_namesFROM
table1FULL OUTER JOIN
table2ON
table1.column_name=table2.column_name;
SELECT
column_namesFROM
table1 T1, table1 T2WHERE
condition;
CREATE VIEW
view_nameAS SELECT
column1, column2FROM
table_nameWHERE
condition;
SELECT
*FROM
view_name;
DROP VIEW
view_name;
ALTER TABLE
table_nameADD
column_name column_definition;
ALTER TABLE
table_nameMODIFY
column_name column_type;
ALTER TABLE
table_nameDROP COLUMN
column_name;