http://www.w3cschool.cn/sql_having.html w3c中有些SQL的讲解
1 order by 排序
SELECT Company, OrderNumber FROM Orders ORDER BY Company;
2 distinct 去重
SELECT DISTINCT Company FROM Orders ;
3 limit 控制选择的条数(MYSQL)
SELECT *FROM Persons LIMIT 5;
ORACLE
SELECT * FROM Persons WHERE ROWNUM <= 5;
4 通配符
SELECT * FROM Persons WHERE LastName LIKE 'C_r_er' -- _代表一个字符
SELECT * FROM Persons WHERE City LIKE '[ALN]%'; -- 以A或L或N 开头的 %代表一个或多个字符 SELECT * FROM Persons WHERE City LIKE '[!ALN]%' -- 不以A或L或N 开头的
5 between
SELECT * FROM Persons WHERE LastName NOT BETWEEN 'Adams' AND 'Carter';
6 union 和union all
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 |
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 |
两个表的叠加 union比union all 多了个排重操作
7 内连接 外连接
A表 B表
id name id name
1 a 1 b
2 b 3 c
4 c
内连接就是左表和右表相同的数据,查询结果只有相等的数据:
select * from A inner join B on A.id=B.id
select * from A,B where A.id=B.id
id name id name
1 a 1 b
外连接分为:左外连接、右外连接、全外连接
左外连接就是以左表为准,去匹配右表,左表有多少条数据,结果就是多少条数据
select * from A left join B on A.id=B.id
id name id name
1 a 1 b
2 b null null
4 c null null
右外连接就是与左外连接反之,以右表为准,去匹配左表,右表有多少条数据,结果就是多少条数据
select * from A right join B on A.id=B.id
id name id name
1 a 1 b
null null 3 c
全外连接数据条数不一定,相当与是左外连接 和右外连接 的综合
select * from A full join B on A.id=B.id
id name id name
1 a 1 b
2 b null null
null null 3 c
4 c null null
8 unique 唯一约束 约束还有 主键 外键 默认值 check(id>0等)不为空
CREATE TABLE Persons ( Id_P int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), UNIQUE (Id_P) ) -- 建表时约束 MYSQL
CREATE TABLE Persons ( Id_P int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) -- ORACLE
9 index 索引
CREATE INDEX PersonIndex ON Person (LastName DESC);
10 alter 只能对表中整进行添加 修改 删除
ALTER TABLE Persons ADD Birthday date; ALTER TABLE Persons ALTER COLUMN Birthday year; ALTER TABLE Person DROP COLUMN Birthday;
11 MYSQL 自增 ORACLE 序列
CREATE TABLE Persons ( P_Id int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) );
CREATE SEQUENCE seq_person MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10;
12 视图 view 视图可以隐藏一些不想让用户看到的信息 以一种新的方式展示数据
create view v as select ename ||' ' ||deptno as data from emp;--创建视图 select * from v;
13 函数
SELECT Customer FROM Orders WHERE OrderPrice>(SELECT AVG(OrderPrice) FROM Orders);
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders;
SELECT FIRST(OrderPrice) AS FirstOrderPrice FROM Orders order by OrderPrice; -- 还有last
SELECT MAX(OrderPrice) AS LargestOrderPrice FROM Orders; -- min()
SELECT SUM(OrderPrice) AS OrderTotal FROM Orders;
SELECT MID(City,1,3) as SmallCity FROM Persons; -- 截取City前3个字符
14 group by 通常和sum() 合用 按group by 的字段分组 如果一个客户有两条记录 将两条记录的数值求和
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer; SELECT Customer,OrderDate,SUM(OrderPrice) FROM Orders GROUP BY Customer,OrderDate;
15 having 和where 唯一区别就是having子句中可以有函数
SELECT Customer,SUM(OrderPrice) FROM Orders GROUP BY Customer HAVING SUM(OrderPrice)<2000;