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 c4 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.idid name id name 1 a 1 b 2 b null null4 c null null右外连接就是与左外连接反之,以右表为准,去匹配左表,右表有多少条数据,结果就是多少条数据select * from A right join B on A.id=B.idid name id name 1 a 1 b null null 3 c全外连接数据条数不一定,相当与是左外连接 和右外连接 的综合select * from A full join B on A.id=B.idid name id name 1 a 1 b 2 b null nullnull null 3 c4 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
时间: 2024-10-26 02:02:33