求指点
时间: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
select t1.*,t2.result from t1 left join t2 on t1.uid=t2.uid where t1.Class=1
作者: roy_88 发布时间: 2011-11-12
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
select t1.*, result=isnull(result,'没有') from t1 left join t2 on t1.uid=t2.uid where t1.Class=1
作者: Beirut 发布时间: 2011-11-12
/* 苦苦的潜行者 */ 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
select uId, class, (select result from 表二 where uId=a.uId) as result from 表一 a where a.class='1'
作者: lxpbs8851 发布时间: 2011-11-12
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
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
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
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28