还是使用很俗套的例子表举例~学生成绩表和班级信息表
CREATE TABLE [dbo].[T_Class] (
[classid] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[classname] [varchar] (50) NULL
) ON [PRIMARY]
GO/*********************/CREATE TABLE [dbo].[T_student] (
[sid] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[sname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[class] [int] NULL ,
[score] [int] NULL
) ON [PRIMARY]
GO
/*---------------------*/
class的数据
classid classname
1 A
2 B
3 C
/*---------------------*/
student的数据
sid sname class score
1 aa1 1 NULL
2 aa2 2 90
3 bb1 2 70
4 aa3 1 80
/******************************/
select classname, score from t_class C
left join t_student S on C.classid = S.class
执行结果
A NULL
A 80
B 90
B 70
C NULL可是……
加上isnull就不对了哇~
select classname, isnull(0, score) as score from t_class C
left join t_student S on C.classid = S.classA 0
A 0
B 0
B 0
C 0====================
怎么解决呢?不太了解原因,晕了~
CREATE TABLE [dbo].[T_Class] (
[classid] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[classname] [varchar] (50) NULL
) ON [PRIMARY]
GO/*********************/CREATE TABLE [dbo].[T_student] (
[sid] [int] IDENTITY (1, 1) NOT FOR REPLICATION NOT NULL ,
[sname] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
[class] [int] NULL ,
[score] [int] NULL
) ON [PRIMARY]
GO
/*---------------------*/
class的数据
classid classname
1 A
2 B
3 C
/*---------------------*/
student的数据
sid sname class score
1 aa1 1 NULL
2 aa2 2 90
3 bb1 2 70
4 aa3 1 80
/******************************/
select classname, score from t_class C
left join t_student S on C.classid = S.class
执行结果
A NULL
A 80
B 90
B 70
C NULL可是……
加上isnull就不对了哇~
select classname, isnull(0, score) as score from t_class C
left join t_student S on C.classid = S.classA 0
A 0
B 0
B 0
C 0====================
怎么解决呢?不太了解原因,晕了~
解决方案 »
- XQuery [modify()]: '00' 附近存在语法错误,应为 ']'
- 100分 求SQL语句,关于随机分组后在随机排序。
- 123
- 问一个关于从表中取数据作为参数返回的SQL语句?
- procedure的问题?
- ======================在线求SQL语句=[本人考虑了一个月了]===========================
- 如何获取当前数据库的所有存储过程名称?(在线)
- object_id(N'[dbo].[ET_Month1_report]')中的N是什么?
- 百分求解:如何进行逻辑运算
- 请告诉我哪里有VFP6.0下载
- 在线等!关于同一表中满足条件后的每条的最近两条记录相减的操作的语句该怎么写?
- 新人请教:请问新建数据表 如何添加图片字段?
left join t_student S on C.classid = S.class
使用指定的替换值替换 NULL。语法
ISNULL ( check_expression , replacement_value ) 参数
check_expression将被检查是否为 NULL的表达式。check_expression 可以是任何类型的。replacement_value在 check_expression 为 NULL时将返回的表达式。replacement_value 必须与 check_expresssion 具有相同的类型。 返回类型
返回与 check_expression 相同的类型。注释
如果 check_expression 不为 NULL,那么返回该表达式的值;否则返回 replacement_value。