如何在SQL Server中查找值 [英] how to find the value in sql server

查看:95
本文介绍了如何在SQL Server中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有一张桌子这样的学生:-

hello,

I have a table e.g. student like this:-

roll    mark
--------------
1       50
2       60
3       70
--------------



当我进行查询时



when I making query

select mark from student where roll=1



它正在显示



It is showing

mark
-----
50



但是当我写的时候



but when I am writing

select mark from student where roll=4



它正在显示



It is showing

mark
-----



但是我想如果条件不满足,那么它返回零意味着:-



But I want if the condition is not satisfied then it return zero means like:-

mark
----
0




谢谢.




Thanks.

推荐答案

这不是一件好事-这意味着您无法分辨不存在的学生与得分的学生之间的区别零.但是:
That is not a nice thing to do - it means you cannot tell the difference between a student that doesn''t exist, and a student that scores zero. However:
IF EXISTS (SELECT mark FROM student WHERE roll=4)
SELECT mark FROM student WHERE roll=4
ELSE
SELECT 0

但是您应该找到一种更好的方式来组织代码.否则,这将是非常低效的!

But you should find a better way to organise your code instead. If nothing else, this is very inefficient!


select isnull((select mark  from @temptable where id=4),0) as mark



该查询基于SQL 2008数据库.@ temptable是表名.



This query is based on SQL 2008 Data Base.@temptable is table name.


这篇关于如何在SQL Server中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆