sql常用语句整理(包括增删改查)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql常用语句整理(包括增删改查)相关的知识,希望对你有一定的参考价值。
转载::CSDN:
http://blog.csdn.net/yjb7268888/article/details/50014393(原文地址)
一、插入(复制)表数据
1、INSERT INTO 语句
(1)插入新的一行数据
- INSERT INTO Persons VALUES (‘Gates‘, ‘Bill‘, ‘Xuanwumen 10‘, ‘Beijing‘);
(2)在指定的列中插入数据
- INSERT INTO Persons (LastName, Address) VALUES (‘Wilson‘, ‘Champs-Elysees‘);
2、SQL SELECT INTO 语句可用于创建表的备份复件
(1)在建表时复制所有数据:
- create table userinfo_new as select * from userinfo;
(2)在建表时复制部分数据:
- create table userinfo_new1 as select id,username from userinfo;
(3)在添加时复制所有数据:
- insert into userinfo_new select * from userinfo;
(4)在添加时复制部分数据:
- insert into userinfo_new(id,username) select id,username from userinfo;
二、修改表数据
1、Update 语句
(1)无条件更新:
- update userinfo set userpwd=‘111‘,email=‘[email protected]‘;
(2)有条件更新:
- update userinfo set userpwd=‘123456‘ where username=‘xxx‘;
三、删除表数据
1、DELETE 语句
无条件删除:
- dalete from userinfo;
有条件删除:
- delete from userinfo where username=‘yyy‘;
四、查询表数据
1、SELECT 语句
(1)查询所有字段:
- select * from users;
(2)查询指定字段:
- select username,salary from users;
2、SELECT DISTINCT 语句
如需从 Company" 列中仅选取唯一不同的值,我们需要使用 SELECT DISTINCT 语句:
- SELECT DISTINCT Company FROM Orders;
3、SQL where
选取居住在城市 "Beijing" 中的人,我们需要向 SELECT 语句添加 WHERE 子句:
- SELECT * FROM Persons WHERE City=‘Beijing‘;
注意:SQL 使用单引号来环绕文本值(大部分数据库系统也接受双引号)。如果是数值,请不要使用引号。
4、SQL AND & OR
(1)使用 AND 来显示所有姓为 "Carter" 并且名为 "Thomas" 的人:
- SELECT * FROM Persons WHERE FirstName=‘Thomas‘ AND LastName=‘Carter‘;
(2)使用 OR 来显示所有姓为 "Carter" 或者名为 "Thomas" 的人:
- SELECT * FROM Persons WHERE firstname=‘Thomas‘ OR lastname=‘Carter‘;
5、ORDER BY 语句用于对结果集进行排序。
(1)以字母顺序显示公司名称:
- SELECT Company, OrderNumber FROM Orders ORDER BY Company;
(2)以字母顺序显示公司名称(Company),并以数字顺序显示顺序号(OrderNumber):
- SELECT Company, OrderNumber FROM Orders ORDER BY Company, OrderNumber;
(3)以逆字母顺序显示公司名称:
- SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC;
(4)以逆字母顺序显示公司名称,并以数字顺序显示顺序号:
- SELECT Company, OrderNumber FROM Orders ORDER BY Company DESC, OrderNumber ASC;
6、TOP 子句
SQL Server 的语法:
从"Persons" 表中选取头两条记录:
- SELECT TOP 2 * FROM Persons;
从"Persons" 表中选取 50% 的记录:
- SELECT TOP 50 PERCENT * FROM Persons;
mysql 语法:
- SELECT * FROM Persons WHERE ROWNUM <= 2;
(1)从"Persons" 表中选取居住在以 "N" 开始的城市里的人:
- 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%‘;
- SELECT * FROM Persons WHERE FirstName LIKE ‘_eorge‘;
- SELECT * FROM Persons WHERE LastName LIKE ‘C_r_er‘;
- SELECT * FROM Persons WHERE City LIKE ‘[ALN]%‘;
- SELECT * FROM Persons WHERE City LIKE ‘[!ALN]%‘;
从表中选取姓氏为 Adams 和 Carter 的人:
- SELECT * FROM Persons WHERE LastName IN (‘Adams‘,‘Carter‘);
以字母顺序显示介于 "Adams"(包括)和 "Carter"(不包括)之间的人:
- SELECT * FROM Persons WHERE LastName BETWEEN ‘Adams‘ AND ‘Carter‘;
注意:不同的数据库对 BETWEEN...AND 操作符的处理方式是有差异的。某些数据库会列出介于 "Adams" 和 "Carter" 之间的人,但不包括 "Adams" 和 "Carter" ;某些数据库会列出介于 "Adams" 和 "Carter" 之间并包括 "Adams" 和 "Carter" 的人;而另一些数据库会列出介于 "Adams" 和 "Carter" 之间的人,包括 "Adams" ,但不包括 "Carter" 。
所以,请检查你的数据库是如何处理 BETWEEN....AND 操作符的!
10、为列名称和表名称指定别名(Alias)- SELECT po.OrderID, p.LastName, p.FirstName FROM Persons AS p, Product_Orders AS po WHERE p.LastName=‘Adams‘ AND p.FirstName=‘John‘;
- SELECT LastName AS Family, FirstName AS Name FROM Persons;
"Persons" 表:
Id_P | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
"Orders" 表:
Id_O | OrderNo | Id_P |
---|---|---|
1 | 77895 | 3 |
2 | 44678 | 3 |
3 | 22456 | 1 |
4 | 24562 | 1 |
5 | 34764 | 65 |
- 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 ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
- SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
Bush | George |
- SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons RIGHT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
34764 |
- SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons FULL JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName;
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
Bush | George | |
34764 |
注意:UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。UNION 结果集中的列名总是等于 UNION 中第一个 SELECT 语句中的列名。
Employees_China:
E_ID | E_Name |
---|---|
01 | Zhang, Hua |
02 | Wang, Wei |
03 | Carter, Thomas |
04 | Yang, Ming |
Employees_USA:
E_ID | E_Name |
---|---|
01 | Adams, John |
02 | Bush, George |
03 | Carter, Thomas |
04 | Gates, Bill |
(1)UNION命令列出所有在中国和美国的不同的雇员名:
- SELECT E_Name FROM Employees_China UNION SELECT E_Name FROM Employees_USA;
E_Name |
---|
Zhang, Hua |
Wang, Wei |
Carter, Thomas |
Yang, Ming |
Adams, John |
Bush, George |
Gates, Bill |
- SELECT E_Name FROM Employees_China UNION ALL SELECT E_Name FROM Employees_USA;
E_Name |
---|
Zhang, Hua |
Wang, Wei |
Carter, Thomas |
Yang, Ming |
Adams, John |
Bush, George |
Carter, Thomas |
Gates, Bill |
(1)选取在 "Address" 列中带有 NULL 值的记录:
- SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NULL;
- SELECT LastName,FirstName,Address FROM Persons WHERE Address IS NOT NULL;
P_Id | ProductName | UnitPrice | UnitsInStock | UnitsOnOrder |
---|---|---|---|---|
1 | computer | 699 | 25 | 15 |
2 | printer | 365 | 36 | |
3 | telephone | 280 | 159 | 57 |
以上是关于sql常用语句整理(包括增删改查)的主要内容,如果未能解决你的问题,请参考以下文章