+ -
当前位置:首页 → 问答吧 → 求指点

求指点

时间:2011-11-12

来源:互联网

表一:
uId class(char型)
1 1
2 1
3 1
4 1
5 2

表二:
uId result(int型)
1 10
2 20

需要的查询结果
uId class result
1 1 10
2 1 20
3 1 没有
4 1 没有

请问如何写查询语句,求指点!

作者: h88566   发布时间: 2011-11-12

SQL code
select 
t1.*,t2.result
from t1
left join t2 on t1.uid=t2.uid
where t1.Class=1

作者: roy_88   发布时间: 2011-11-12

SQL code

if object_id('表一','U') is not null
   drop table 表一
go
create table 表一
(
 uId int,
 class char(10)
)
go
insert into 表一
select 1,1 union all
select 2,1 union all
select 3,1 union all
select 4,1 union all
select 5,1
go
if object_id('表二','U') is not null
   drop table 表二
go
create table 表二
(
 uid int,
 result int
) 
go
insert into 表二
select 1,10 union all
select 2,20
go

select a.uId,class,result = case when result is null then '没有' else cast(result as varchar) end from 表一 a left join 表二 b on a.uId=b.uId
/*
uId         class      result
----------- ---------- ------------------------------
1           1          10
2           1          20
3           1          没有
4           1          没有
5           1          没有

(5 行受影响)


*/

作者: pengxuan   发布时间: 2011-11-12

SQL code
select 
t1.*,
result=isnull(result,'没有')
from t1
left join t2 on t1.uid=t2.uid
where t1.Class=1

作者: Beirut   发布时间: 2011-11-12

SQL code
/*
苦苦的潜行者
*/
if object_id('t1') is not null and object_id('t2') is not null
drop table t1,t2
create table t1 (uId int ,class char(2))
insert t1
select 1,'1' union all
select 2,'1' union all
select 3,'1' union all
select 4,'1' union all
select 5,'2'
create table t2 (uId int,result int)
insert t2
select 1,10 union all
select 2,20
go
select a.*,b.result from 
(select * from t1 where class='1') a 
left join t2 b on a.uId=b.uId
/*
(所影响的行数为 4 行)
uId  class result
---  -----  ----
1     1    10
2     1    20
3     1    null
4     1    null
*/
go 
drop table t1,t2

作者: xiaolinyouni   发布时间: 2011-11-12

SQL code

select uId, class,
(select result from 表二 where uId=a.uId) as result
from 表一 a 
where a.class='1'

作者: lxpbs8851   发布时间: 2011-11-12

SQL code
select a.uid,a.class,case when isnull(b.result,0)=0 then '没有'else convert(char,b.result) end  as Result from t1 a left join t2 b  on a.uid=b.uid where a.class=1

作者: Tosp2012   发布时间: 2011-11-12

引用 2 楼 pengxuan 的回复:

SQL code

if object_id('表一','U') is not null
drop table 表一
go
create table 表一
(
uId int,
class char(10)
)
go
insert into 表一
select 1,1 union all
select 2,1 union all
select 3,1 union all
select……

谢谢指点,还有请问如果查询结果是
uId class result rank(排名)
1 1 20 1
2 1 20 1
3 1 10 2
4 1 没有 没有

作者: h88566   发布时间: 2011-11-12

SQL code

select t1.*,case when t2.result is null then '没有' else t2.result end as result,
case when t2.result is null then '没有' else
dense_rank() over(order by t2.result) end as [rank]
from t1 left join t2 on t1.uid=t2.uid 
where t1.Class=1

作者: lxpbs8851   发布时间: 2011-11-12

引用 8 楼 lxpbs8851 的回复:

SQL code

select t1.*,case when t2.result is null then '没有' else t2.result end as result,
case when t2.result is null then '没有' else
dense_rank() over(order by t2.result) end as [rank]
from t1 left ……

要是用sqlserver2000,这个dense_rank()函数不行?怎办?

作者: h88566   发布时间: 2011-11-12