【原博客自己写于CSDN。网址https://blog.csdn.net/basic_wheels96/article/details/107229725】
说明
1:虽然SQL不区分大小写,个人偏向全部大写。部分表名,列名和值用原本的写法。
2:不缩写,比如AS关键字。
3:结尾写';'
语法参考链接:https://www.w3school.com.cn/sql/index.asp
关键字
INSERT
DELETE
CASE
UNION
ORDER BY
GROUP BY
HAVING
FULL JOIN
EXISTS
不用JOIN的两表关联
WHERE+IN
DELETE+WHERE+IN
INSERT
模板1:INSERT INTO TABLE_NAME (column1,column2) VALUES(value1, value2);
模板2:INSERT INTO TABLE_NAME1 (column1,column2) (SELECT column1, column2 FROM TABLE_NAME2);
例子1:INSERT INTO Persons VALUES ('Gates', 'Bill', 'Xuanwumen 10', 'Beijing');
例子2:INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees');
数据库参考:https://www.w3school.com.cn/sql/sql_insert.asp
DELETE
例子:DELETE FROM Person WHERE LastName = 'Wilson';
数据库参考:https://www.w3school.com.cn/sql/sql_delete.asp
CASE
模板:
SELECT (CASE WHEN COLUMN1 = ' ' THEN ' '
WHEN COLUMN1 = ' ' THEN ' '
ELSE ' ' END) AS
FROM TABLE_NAME1;
例子:
SELECT (CASE WHEN CHG_TYPE = '0' THEN 'A'
WHEN CHG_TYPE = '1' THEN 'B'
ELSE 'C' END) AS TEST
FROM TTRD;
注:' ' 表示里面需要填参数。
UNION
一句话说明:用来合并两个或多个 SELECT 语句的结果集。
例子:
SELECT E_Name FROM Employees_China
UNION
SELECT E_Name FROM Employees_USA;
注:和UNION ALL区别是,UNION ALL会显示所有信息,包括重复。UNION会覆盖重复信息,只保留一条。
数据库参考:https://www.w3school.com.cn/sql/sql_union.asp
ORDER BY
例子:SELECT Company, OrderNumber FROM Orders ORDER BY Company;
注:ORDER BY默认升序排列,就是从A到Z排序。可以加'DESC'关键字,来实现Z到A排序。
比如SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC;
数据库参考:https://www.w3school.com.cn/sql/sql_orderby.asp
GROUP BY
一句话说明:结果集进行分组。
例子:
SELECT Customer, SUM(OrderPrice) FROM Orders
GROUP BY Customer;
数据库参考:https://www.w3school.com.cn/sql/sql_groupby.asp
HAVING
一句话说明:WHERE 关键字无法在SUM函数中使用。HAVING起到了WHERE的筛选作用。
例子:
SELECT Customer, SUM(OrderPrice) FROM Orders
GROUP BY Customer
HAVING SUM(OrderPrice)<2000;
数据库参考:https://www.w3school.com.cn/sql/sql_having.asp
FULL JOIN
一句话说明:两个表JOIN理解为两个圈相交。取两个圈的全部内容。
模板:
SELECT COLUMN FORM TABLE_NAME1
FULL JOIN TABLE_NAME2
ON ' '=' ' ORDER BY ;
例子:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
FULL JOIN Orders
ON Persons.Id_P=Orders.Id_P
ORDER BY Persons.LastName;
注:ORDER BY默认升序排列,就是从A到Z排序。可以加'DESC'关键字,来实现Z到A排序。
数据库参考:https://www.w3school.com.cn/sql/sql_join_full.asp
EXISTS
一句话说明:显示子查询结果。
例子:SELECT * FROM a WHERE EXISTS(SELECT id FROME a WHERE name='a');
注:对于单个EXISTS,子查询返回null,则结果为null。子查询非null,则查询显示输出结果。
数据库参考:https://www.cnblogs.com/lvcha001/p/10312870.html
不用JOIN的两表关联
一句话说明:JOIN被WHERE中的'='替代。
例子:
SELECT A.* FROM TTRD_ACCOUNTING_SECU_OBJ AS A, TTRD_ACCOUNTING_SECU_OBJ_HIS AS B
WHERE A.TSK_ID = B.TSK_ID;
注:自己理解是这里的'='只能起到INNER JOIN的作用。
数据库参考:无
WHERE+IN
一句话说明:条件多值查询。
例子:SELECT * FROM PERSONS WHERE LASTNAME IN ('Adams','Carter');
数据库参考:https://www.w3school.com.cn/sql/sql_in.asp
DELETE+WHERE+IN
说明:目前没有人教过可以这样写。是我自己学完WHERE+IN可以实现多值操作。那么理论上WHERE+IN还可以和别的关键字组合形成批量操作。比如和DELETE组合。实现多行删除。
例子:DELETE ' ' FROM PERSON WHERE LASTNAME IN ('Wilson' ,'Gates');
数据库参考:https://www.w3school.com.cn/sql/sql_in.asp
后记
如有错误请指出,谢谢。