1.针对相同的表进行连接的技术称为自连接。
2.集合是SQL能处理唯一的数据结构。
3.自连接的性能开销很大(特别是与非等值连接结合使用的时候,用于自连接的列推荐使用主键或者相关列上建立索引)。

select p1.name as name_1, p2.name as name_2 from Products p1, Products p2
where p1.name >= p2.name


方法一:窗口函数
select district, name, price, rank() over (partition by distinct order by price DESC) as rank_1
from DistrictProducts;
partitition by 具体将表分割成若干个小的子集的作用。因为本题以地区作为分割条件,所以指定distinct列。
方法二:标量子查询
select p1.district, p1.name, p1.price,
(select count(p2.price) from DistrictProducts p2
where p1.district = p2.district --在同一个地区内进行比较
and p2.price > p1.price) + 1 as rank_1
from DistrictProducts p1;
方法三:自连接
select p1.district, p1.name, max(p1.price) as price, count(p2.name)+1 as rank_1
from DistrictProducts p1 left outer join DistrictProducts p2
on p1.district = p2.district
and p1.price < p2.price
group by p1.district,p1.name


UPDATE DistrictProducts2 P1
SET ranking = (SELECT COUNT(P2.price) + 1
FROM DistrictProducts2 P2
WHERE P1.district = P2.district
AND P2.price > P1.price);
方法一:在update语句的set子句中加入计算位次的逻辑
update DistrictProducts1 p1 set ranking = (select count(p2.price) + 1 from DistrictProducts2 p2
where p1.district = p2.district and p2.price > p1.price )
方法二:窗口函数
update DistrictProducts2 set ranking = rank() over(partition by district order by price desc)