SQL复习
hive中的各种join操作
join是最简单的关联操作,两边关联只取交集。
outer join分为left outer join、right outer join和full outer join。
left outer join是以左表驱动,右表不存在的key均赋值为null;
right outer join是以右表驱动,左表不存在的key均赋值为null;
full outer join全表关联,将两表完整的进行笛卡尔积操作,左右表均可赋值为null。
create table if not exists banning(
train_id string comment '培训id',
dri_id string comment '司机id'
)
partitioned by(dt string)
row format delimited fields terminated by '\t'
stored as textfile;
select
pag_id,
tag_name
from
(
select
row_number() over
(partition by pag_id order by gmt_crate desc) as rownumbers
from dj_pas_result
)tag_name_tmp
where tag_name_tmp.rownumbers=1
hive中inner join和left semi join 区别(semi join是解决hive中不支持exist in 方法
eg:
Suppose there are 2 tables TableA and TableB with only 2 columns (Id, Data) and following data:
TableA:
Id Data
1 DataA11
1 DataA12
1 DataA13
2 DataA21
3 DataA31
TableB:
Id Data
1 DataB11
2 DataB21
2 DataB22
2 DataB23
4 DataB41
Left Semi Join on column Id will return columns only from left table and matching records only from left table:
Id Data
1 DataA11
1 DataA12
1 DataA13
2 DataA21
Inner Join on column Id will return columns from both the tables and only the matching records:
Id Data Id Data
1 DataA11 1 DataB11
1 DataA12 1 DataB11
1 DataA13 1 DataB11
2 DataA21 2 DataB21
2 DataA21 2 DataB22
2 DataA21 2 DataB23
1、select
SELECT LastName,FirstName FROM Persons
SELECT * FROM Persons
2、distinct
SELECT DISTINCT Company FROM Orders
3、WHERE
SELECT * FROM Persons WHERE city='beijing'
4、AND&OR运算符
SELECT * FROM Persons WHERE FirstName='tomas' AND LastName='carp'
SELECT * FROM Persons WHERE FirstName='tomas' OR LastName='carp'
5、ORDER BY
默认是升序,可以自己添加ASC 和DESC
SELECT Company,OrderNumber FROM Orders ORDER BY Company,OrderNumber //首先按照Company升序排列,在Company相同的时候按照OrderNumber关键字升序排列
SELECT Company,OrderNumber FROM Orders ORDER BY Company DESC,OrderNumber ASC //首先按照Company降序排列,在Company相同的时候按照OrderNumber关键字升序排列
6、insert
插入新的行 INSERT INTO Person VALUES('gate','bill','xuanwumen 10','beijing')
插入指定列中的数据 INSERT INTO Person(LastName,Address) VALUES('wilson','champli stree')
7、update
改变表中的元素值
update Person SET FirstName='xiangli' WHERE LastName='wilson'
修改一行中的多个列
update Person SET City='anhui',Address='chinal addre' WHERE LastName='wilson'
8、delete
删除表中的行
DELETE FROM Person WHERE LastName='wilson'
删除所有行
DELETE * FROM Person
9、TOP语句
Mysql语句中的TOP
SELECT * FROM Persons LIMIT 5
SQL中的TOP
SELECT TOP 2 * FROM Persons
SELECT TOP 50 PERCENT * FROM Persons //选取前50%
10、通配符
% 替代一个或者多个字符
_替代一个字符
[charlist] 字符列中的任意单一字符
[^charlist]或者[!charlist] 不在字符列中任意单一字符
SELECT * FROM Persons WHERE City LIKE '[ALN]%' //选取City 以‘A','L','N'开头
11、in
SELECT * FROM Persons WHERE LastNames IN ('Adms','Carter') //选取姓氏是adms,carter的人
12、BETWEEN AND
SELECT * FROM Persons WHERE LastName Between 'Adam' AND 'Carter' //字母表顺序排序,在adam和carter(不包括)之间的人
13、使用别名
对表使用别名
SELECT p.ordernumber,po.LastName From Orders AS p,Products AS po WHERE p.LastName='Adam' AND p.FirstName='herry'
对列使用别名
SELECT LastName AS Fambily, FirstName AS Name FROM Persons //返回的标的名字就变成了Fambily,Name
14、连接
内连接(只要两个表中有匹配的,就返回该行)
SELECT Persons.LastName,Persons.FirstName,Orders.OrderNo FROM Persons,Orders WHERE Persons.Id_p=Order.Id_p
用内连接也就是SELECT Persons.LastName,Persons.FirstName,Orders.OrderNo FROM Persons INNER JOIN Order ON Persons.Id_p=Order.Id_p
左连接(即使右表没有匹配的项,也要返回左表中的所有内容)
SELECT Persons.LastName,Persons.FirstName,Orders.OrderNo FROM Persons LEFT JOIN Order ON Persons.Id_p=Order.Id_p //将返回左表Person中的所有LastName和FirstName选项
右连接(即使左表没有匹配的项,也要返回右表中的所有内容)
SELECT Persons.LastName,Persons.FirstName,Orders.OrderNo FROM Persons RIGHT JOIN Order ON Persons.Id_p=Order.Id_p //将返回右表Orders中的所有OrderNo选项
全连接(列出左表和右表中的所有行,即使两个表没有匹配的选项,实际即使左连接和右连接的结合)
SELECT Persons.LastName,Persons.FirstName,Orders.OrderNo FROM Persons FULL JOIN Order ON Persons.Id_p=Order.Id_p //将返回左表Person中的所有LastName和FirstName选项,返回右表Orders中的所有OrderNo选项
15、union
union合并(如果有相同的值,只选取一个)
union all 合并(如果有相同的值,都选取)
SELECT E_Name FROM Employees_china
UNION
SELECT E_Name FROM Employees_USA
16、select into
从一个表(或者多个表)中选取数据插入到另外一个表
SELECT LastName,FirstName INTO Person_backup FROM Persons
17、CREATE DATABASE
CREATE DATABASE my_db
18、Create table
数据类型:
整型
integer(size),int(size),smallint(size),tinyint(size)
小数
numeric(size,d)(size最大的位数,d小数点右侧的位数),decimal(size,d)
字符
char(size),varchar(size)
日期
data(yyyymmdd)
CREATE TABLE Persons
(
Id_p int,
LastName char(255),
FirstName char(255)
)
19、sql约束
NOT NULL 不容许为空置
CREATE TABLE Persons
(
Id_p int NOT NULL,
LastName char(255)
)
UNIQUE
mysql中
CREATE TABLE Persons
(
Id_p int NOT NULL,
LastName char(255),
UNIQUE(Id_p)
)
SQL Server/Oracle
CREATE TABLE Persons
(
Id_p int NOT NULL UNIQUE,
LastName char(255)
)
命名unique约束,并且为多个列添加unique约束
mysql/sql
CREATE TABLE Persons
(
Id_p int NOT NULL,
LastName char(255),
CONSTRAINT uc_PersonID UNIQUE(Id_p,LastName)
)
为表中的某个属性添加约束
ALTER TABLE Persons ADD UNIQUE(Id_p)
ALTER TABLE Persons ADD CONSTAINT uc_PersonID UNIQUE(Id_p,LastName)
撤销表中的每个约束
mysql
ALTER TABLE Persons DROP INDEX uc_PersonID
sql
ALTER TABLE Persons DROP CONSTRAINT uc_PersonID
主键
mysql
CREATE TABLE Persons
(
Id_p int NOT NULL,
LastName char(255),
PRIMARY KEY (Id_p)
)
SQL Server/Oracle
CREATE TABLE Persons
(
Id_p int NOT NULL PRIMARY KEY,
LastName char(255)
)
定义多个列的primary key
mysql/sql
CREATE TABLE Persons
(
Id_p int NOT NULL ,
LastName char(255),
CONSTRAINT pk_PersonID PRIMARY KEY(Id_p,LastName)
)
修改表格添加primary key
ALTER TABLE Persons ADD PRIMARY KEY(Id_p)
ALTER TABLE Persons ADD CONSTRAINT pk_PersonId PRIMARY KEY(Id_p,LastName)
撤销primary key
mysql
ALTER TABLE Persons DROP PRIMARY KEY(Id_p)
sql
ALTER TABLE persons DROP CONSTRAINT pk_PersonId
外键
mysql
CREATE TABLE Orders
(
Id_p int NOT NULL ,
LastName char(255),
FPREIGN KEY (Id_p) REFERENCES Persons(Id_p)
)
sql
CREATE TABLE Orders
(
Id_p int FPREIGN KEY REFERENCES Persons(Id_p),
LastName char(255)
)
定义foreign key
mysql/sql
CREATE TABLE Orders
(
Id_p int ,
LastName char(255),
CONSTRAINT fk_OrdersNo FOREIGN KEY(Id_p) REFERENCES Persons(Id_p)
)
改变表格添加foreign key
ALTER TABLE Orders ADD FOREIGN KEY (Id_p) REFERENCES Persons(Id_p)
ALTER TABLE Orders ADD CONSTRAINT fk_OrdersNo FOREIGN KEY(Id_p) REFERENCES Persons(Id_p)
撤销foreign key
mysql
ALTER TABLE Orders DROP FOREIGN KEY fk_OrdersNo
sql
ALTER TABLE Orders DROP CONSTRAINT fk_OrdersNo
check约束
mysql
CREATE TABLE Persons
(
Id_p int ,
LastName char(255),
CHECK (Id_p>0)
)
sql
CREATE TABLE Persons
(
Id_p int CHECK (Id_p>0) ,
LastName char(255),
)
给约束加名字
mysql/sql
CREATE TABLE Persons
(
Id_p int ,
LastName char(255),
City varchar(255),
CONSTRAINT ck_person CHECK (Id_p>0 AND City='longdon')
)
修改表格添加check
ALTER TABLE Persons Add CHECK (Id_p>0)
ALTER TABLE Persons Add CONSTRAINT ck_person CHECK(Id_p>0)
去掉check
mysql
ALTER TABLE Persons DROP CHECK ck_person
sql
ALTER TABLE Persons DROP CONSTRAINT ck_person
DEFAULT约束
CREATE TABLE Persons
(
Id_p int,
City varchar(255) DEFAULT 'sandes'
)
改变表格添加DEFAULT
mysql
ALTER TABLE Persons ALTER City SET DEFAULT 'sandes'
sql
ALTER TABLE Persons ALTER COLUMN City SET DEFAULT 'sandes'
丢掉DEFAULT
mysql
ALTER TABLE Person ALTER City DROP DEFAULT
sql
ALTER TABLE Person ALTER COLUMN City DROP DEFAULT
20、创建索引
CREATE INDEX PersonIdx ON Person(LastName)
丢弃索引
MYSQL
ALTER TABLE Person DROP INDEX personIdx
21、drop
如果仅仅丢弃表格中的数据
TRUNCATE TABLE Person
22、alter
增加新列
sql
ALTER TABLE Person ADD Birstday data
改变列的数据类型
sql
ALTER TABLE Person ALTER COLUMN Bristday year
删除birstday
ALTER TABLE Person DROP COLUMN Birstday
23、视图
CREATE VIEW [Category sales list] AS SELECT ProductName,price FROM Poducts
24、is null 和is not null
用IS NULL 和IS NOT NULL 来测试NULL数据
SELECT LastName FROM Person WHERE City IS NULL
25、AVG()
SELECT AVG(Price) AS OrderAverage FROM Orders
结果
OrderAverage |
---|
950 |
26、count()
SELECT COUNT(Custormer) AS countcustormer FROM Orders
SELECT COUNT(DISTINCT Custormer) AS countcustormer FROM Orders
27、first(),last(),max(),min(),sum()
SELECT FIRST(Orderprice) AS FirstPrice FROM Orders
SELECT LAST(Orderprice) AS LastPrice FROM Orders
SELECT MAX(Orderprice) AS MAXPrice FROM Orders
SELECT MIN(Orderprice) AS MinPrice FROM Orders
SELECT SUM(Orderprice) AS SumPrice FROM Orders
28、group by
SELECT Customer,SUM(Orderprice) FROM Orders GROUP BY Customer
结果
Customer | SUM(OrderPrice) |
---|---|
Bush | 2000 |
Carter | 1700 |
Adams | 2000 |
29、having
SELECT Customer,SUM(Orderprice) FROM Orders GROUP BY Customer HAVING SUM(Orderprice)>1200
30、ucase(),lcase()
SELEC UCASE(FirstName) AS Famlily FROM Persons //将所有大写转换成小写
SELEC LCASE(FirstName) AS Famlily FROM Persons //将所有小写转换成大写
31、MID(列名,开始位置,提取字符的个数)
SELECT MID(City,1,3) AS Smallcity FROM Persons
32、len()返回文本中字段的长度
SELECT LEN(City) AS Lencity FROM Persons
33、round(列名,小数的位数)
SELECT Productname,round(price,0) AS UNIPrice FROM Products
34、now()获取当前日期和时间
SELECT Productname,NOW() AS PerDate FROM Products
35、format()对字段格式化
SELECT Productname,FORMAT(NOW(),'YYYY-MM-DD') AS PerDate FROM Products
mysql中没有select into 创建新表,取而代之的是
create table t1(select * from t)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步