如何在sql server中使用true,false和两个条件 [英] How to use true, false and both condition in sql server

查看:591
本文介绍了如何在sql server中使用true,false和两个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的设计中有一个复选框(Active)。它存储0或1.我必须根据复选框值搜索项目,即,活动或非活动或两者。对于活动我可以使用1而对于非活动我可以使用0.但我需要使用所有条件(1,0,1和0)。

解决方案

你是什么谈论三态问题的声音。

复选标记已设置== 1

复选标记未设置== 0

复选标记既未设置也未设置= =第三状态。



将第三个状态存储在数据库中的一种方法是允许该列为NULL。





您可以这样检查:



  SELECT  * 
FROM MyTable mt
WEHRE mt.TriStateSomething = NULL





干杯!


那是有点奇怪:一个数字不能同时为0和1,所以搜索为零和是一将永远失败,因为如果一个测试通过,另一个自动y失败,所以结果总是不匹配。

由于该值只能是0或1,因此搜索为零或为1也是正确的永远是这些价值观之一。



这就像掷硬币一样:如果你选择头和尾,你就会失败 - 因为硬币会是一个或者另一方面,如果你选择无论是头还是尾,你都会赢,因为它总会显示出一个或另一个。 (如果你在现实世界中尝试这个,你可能会受到打击;))。



我认为你需要重新考虑你想要做什么 - 我'我不确定你是不是很低调,或者你已经解释得非常糟糕! :笑:


Quote:

我必须根据复选框值搜索项目,即。,无论是活动的还是非活动的,或两者兼而有。

如果您只是按照上面的伪代码进行操作,那么您实际上是在说'



  WHERE  checked =  1   checked =  0  





这真的意味着你根本不需要支票


Hi,
I have a check box in my design (Active). It stores either 0 or 1. I have to search items on the basis of check box value, ie., either active or inactive or both. For active I can use 1 and for inactive I can use 0. But I need to use all the condition (1,0,1&0).

解决方案

What you are talking about sounds like tri-state problem.
The checkmark has been set == 1
The checkmark has been unset == 0
The checkmark neither been set nor unset == third state.

One way to store this third state in a DB would be allowing that column to be NULL.


You could check for this like so:

SELECT *
FROM MyTable mt
WEHRE mt.TriStateSomething = NULL



Cheers!


That's a little odd: a number can't be both 0 and 1 at the same time, so searching for "is zero" and "is one" will always fail, because if one test passes, the other automatically fails, so the result is always "no match".
And since the value can only be 0 or 1, searching for "is zero" or "is one" will also be true since the value will always be one of those values.

It's like tossing a coin: If you choice "Both heads and tails" you will lose - because the coin will be one or the other, and if you chose "either heads or tails" you will win because it will always show one or the other. (You might get punched if you try this in the real world though ;) ).

I think you need to rethink exactly what you are trying to do - I'm not sure you quite undersatnd, or you have explained it very badly! :laugh:


Quote:

I have to search items on the basis of check box value, ie., either active or inactive or both.

If you just follow your "pseudo" code above you are really saying

WHERE checked = 1 OR checked = 0



which really means you don't need the check at all.


这篇关于如何在sql server中使用true,false和两个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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