时间:2021-07-01 10:21:17 帮助过:2人阅读
排序: order by
SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC
插入:insert into
INSERT INTO 表 VALUES (值1, 值2,....) INSERT INTO 表 (列1, 列2,...) VALUES (值1, 值2,....)
更新:update
UPDATE Person SET Address = ‘Zhongshan 23‘, City = ‘Nanjing‘ WHERE LastName = ‘Wilson‘ 删除: delete from DELETE FROM Person DELETE FROM Person WHERE LastName = ‘Wilson‘ TOP: SELECT TOP 2 * FROM Persons SELECT TOP 50 PERCENT * FROM Persons LIKE,NOT LIKE SELECT * FROM Persons WHERE City LIKE ‘N%‘ SELECT * FROM Persons WHERE City LIKE ‘%g‘ SELECT * FROM Persons WHERE City LIKE ‘%lon%‘ SELECT * FROM Persons WHERE City NOT LIKE ‘%lon%‘ 通配符: % 替代一个或多个字符 _ 替代一个字符 IN: SELECT * FROM Persons WHERE LastName IN (‘Adams‘,‘Carter‘) BETEEN AND, NOT BETEEN AND SELECT * FROM Persons WHERE LastName BETWEEN ‘Adams‘ AND ‘Carter‘ 别名:AS SELECT LastName AS Family, FirstName AS Name FROM Persons SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons, Orders WHERE Persons.Id_P = Orders.Id_P UNION:合并两个或多个 SELECT 语句的结果集 注意,内部的 SELECT 语句必须拥有相同数量的列 默认地,UNION 操作符选取不同的值。如果允许重复的值,请使用 UNION ALL SELECT column_name(s) FROM table_name1 UNION SELECT column_name(s) FROM table_name2 TRUNCATE TABLE:除去表内的数据,但并不删除表本身 truncate table 表 函数: AVG() SELECT AVG(OrderPrice) AS OrderAverage FROM Orders SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders) COUNT(*),COUNT(列),COUNT(distinct 列) SELECT COUNT(Customer) AS CustomerNilsen FROM Orders WHERE Customer=‘Carter‘ SELECT COUNT(*) AS NumberOfOrders FROM Orders SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
FIRST() SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders
LAST() SELECT LAST(OrderPrice) AS LastOrderPrice FROM Orders
MAX()
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders MIN() SELECT MIN(OrderPrice) AS SmallestOrderPrice FROM Orders SUM() SELECT SUM(OrderPrice) AS OrderTotal FROM Orders GROUP BY 合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句 SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer GROUP BY 一个以上的列 SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate HAVING 子句 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用 SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000
SELECT Customer,SUM(OrderPrice) FROM Orders WHERE Customer=‘Bush‘ OR Customer=‘Adams‘ GROUP BY Customer HAVING SUM(OrderPrice)>1500
sql语句
标签:sql