--> 测试数据:[mb]
if object_id('[mb]') is not null drop table [mb]
go 
create table [mb]([mbid] int,[mbsort] int,[mbpic] varchar(5),[mdid] int)
insert [mb]
select 1,2,'a.jpg',1 union all
select 2,-1,'b.jpg',1 union all
select 3,0,'c.jpg',1 union all
select 4,-2,'d.jpg',3 union all
select 5,5,'e.jpg',2 union all
select 6,2,'f.jpg',3 union all
select 7,1,'g.jpg',2 union all
select 8,0,'h.jpg',2 union all
select 9,0,'i.jpg',4
--> 测试数据:[md]
if object_id('[md]') is not null drop table [md]
go 
create table [md]([mdid] int,[mdname] varchar(4))
insert [md]
select 1,'小马' union all
select 2,'群群' union all
select 3,'小张' union all
select 4,'阿力'--查找:相同mdid的,只显示一条数据,而这条数据是mbsort为最小的一条--做法1:
select * 
from mb a
where exists(select 1 
             from (select mdid,min(mbsort) as mbsort
                   from mb
                   group by mdid
                  ) b
             where a.mbsort = b.mbsort
               and a.mdid = b.mdid
            )--做法2:
select * from mb a where mbsort = (select min(mbsort) from mb where mdid = a.mdid)--问题:做法1和做法2那个快一点?数据量增加时是否会有性能变化?
--另外怎么看懂执行计划,我打开了执行计划,但是不知道两个语句的执行计划怎么比较--第二个小问题:
select num_col,char_col
from #temp
order by 1 desc
order by 1 desc 是根据什么排序?

解决方案 »

  1.   

    order by 1 desc
    order by 1 desc 是根据什么排序?
    你可以查帮助,
    按你查询的第一列排序
      

  2.   

    1.做法2效率高些
    2.order by 1 descorder by 1,2 desc 如果这样说你牛明白了,先按第1列排序,然后按第2列
      

  3.   

    order_by_expression指定要排序的列。可以将排序列指定为列名或列的别名(可由表名或视图名限定)和表达式,或者指定为代表选择列表内的名称、别名或表达式的位置的负整数