SELECT * FROM 表名 WHERE 条件句 ORDER BY 排序字段 LIMIT 1 或者
十年的金华网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整金华建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“金华网站设计”,“金华网站推广”以来,每个客户项目都认真落实执行。
SELECT * FROM 表名 WHERE 条件句 ORDER BY 排序字段 LIMIT 0,1
注意,LIMIT 要放在末尾。
数据取出来有没有排序关系?以下以id为排序字段
with t1(a,b,id) as (select 5,2,1 union all select 2,3,1 union all select 5,4,1),
t2(a,b,r) as (select a,b,row_number() over(order by id) from t1),
t3 as (select a,b, r, a-b c from t2 where r=1
union all select t2.a,t2.b,t2.r,t3.c+t2.a-t2.b from t3 join t2 on t2.r=t3.r+1)
select a,b,c from t3
SQL2005写法。
例如求出每个部门最高薪酬的人员信息。跟你每组第一行的要求很类似吧!
Declare @Employee table (ID int ,Dept int ,Salary decimal(10,2))
insert into @Employee values(1,10,5500.00)
insert into @Employee values(2,10,4500.00)
insert into @Employee values(3,20,1900.00)
insert into @Employee values(4,20,4800.00)
insert into @Employee values(5,40,6500.00)
insert into @Employee values(6,40,14500.00)
insert into @Employee values(7,40,44500.00)
insert into @Employee values(8,50,6500.00)
insert into @Employee values(9,50,7500.00)
--SELECT * FROM @Employee
SELECT
ID,Dept,Salary
FROM
(
SELECT
ROW_NUMBER() OVER (partition by Dept ORDER BY Salary desc) as Rank,
ID,Dept,Salary
FROM @Employee
) M
WHERE Rank=1
-----
执行结果:
---------
1 10 5500.00
4 20 4800.00
7 40 44500.00
9 50 7500.00
整个语句要一起执行,分开执行是不行的
如果你是先执行create database hello
那么文件配置on primary为系统默认值,你再执行on primary语句就会不断报on错误