插入:
INSERT INTO 表名称 VALUES (值1, 值2,....)
INSERT INTO 表名称 (列1, 列2,...) VALUES (值1, 值2,....)
INSERT OR REPLACE INTO 表名称 VALUES (值1, 值2,....)
INSERT INTO Persons (LastName, Address) VALUES ('Wilson', 'Champs-Elysees')
删除:
DELETE FROM 表名称 WHERE 列名称 = 值
DELETE FROM table_name
修改:
UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值
UPDATE Person SET Address = 'Zhongshan 23', City = 'Nanjing' WHERE LastName = 'Wilson'
查询:
SELECT 语句用于从表中选取数据。
结果被存储在一个结果表中(称为结果集)
SELECT * FROM 表名称
SELECT 列名称 FROM 表名称
SELECT column_name AS alias_name FROM table_name
SELECT TOP number|percent column_name(s) FROM table_name
SELECT * FROM Persons LIMIT 5 //top 与 limit等价
SELECT DISTINCT 列名称 FROM 表名称 //重复只出一个
SELECT 列名称 FROM 表名称 WHERE 列 运算符 值 SELECT * FROM Persons WHERE FirstName='Thomas' AND
LastName='Carter'
SELECT * FROM Persons WHERE City LIKE 'N%' //通配符有 % _ [!ANC]A,N,C中的一个
SELECT column_name(s)FROM table_name WHERE column_name IN (value1,value2,...)
SELECT column_name(s)FROM table_name WHERE column_name BETWEEN value1 AND value2
SELECT Company, OrderNumber FROM Orders ORDER BY 列名 [DESC ASC]
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer
连合查询
join 用于根据两个或多个表中的列之间的关系,从这些表中查询数据。
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons, Orders
WHERE Persons.Id_P = Orders.Id_P
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo
FROM Persons
INNER JOIN Orders
ON Persons.Id_P = Orders.Id_P
INNERJOIN: 如果表中有至少一個匹配,則返回行
LEFT JOIN: 即使右表中沒有匹配,也從左表返回所有的行
RIGHT JOIN: 即使左表中沒有匹配,也從右表返回所有的行
FULL JOIN: 只要其中一個表中存在匹配,就返回行(mysql不支持,可用union
mysql> SELECT * FROM a LEFT JOIN b ON a.id=b.id
-> UNION
-> SELECT * FROM a RIGHT JOIN b ON a.id=b.id;
)
拷贝表
SELECT * FROM Persons
IN 子句可用于向另一个数据库中拷贝表:
SELECT * INTO Persons IN 'Backup.mdb' FROM Persons
电信100元仅售98.60
联通100仅售99.00