如果特定列的记录在数据库中相同,如何返回true(1)或false(0) [英] How to return true(1) or false(0) if a the records of a particular column are the same in the database

查看:216
本文介绍了如果特定列的记录在数据库中相同,如何返回true(1)或false(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张看起来像这样的桌子





i have a table that looks like this


CodeID                   Status
         
         FO1                      FREE
         
         FO2                      NOT FREE
        
         FO1                      FREE
        
         FO1                      FREE
        
         FO1                      FREE



我想要实现的是检查上面所有FO1是否都具有Free状态。如果是免费的,我想执行和行动。我通过方式使用asp.net Web表单。请帮我解决如何构建我的查询,以便能够判断我的特定值的所有记录是否相同



我尝试过:




What i want to achieve is check if in the above above all FO1 have status of Free. If are free, i want to carry out and action. I'm using asp.net web form for the application by way. Kindly help me out with how to structure my query to be able to tell if my all records for a particular value are the same

What I have tried:

select count(distinct Status) from TableName where CodeID = 'FO2' and Status ='FREE'







我在SQL服务器上测试这个查询只是为了确定我是不是做得对。上面的查询返回1但是如果我将其中一个FO1更改为NOT FREE,则上面的代码会返回1.我想让它返回o。




I'm testing with this query in SQL server just to be sure if what i'm doing is right. The above query returns 1 but if i change one of the FO1 to NOT FREE the above code stills returns 1. I want to make it return o.

推荐答案

的方式我这样做是为了使用GROUP BY:

The way I'd do it is to use GROUP BY:
SELECT COUNT(Status) FROM TableName GROUP BY CodeID, Status HAVING CodeID = 'F01' AND Status != 'FREE'

A值为0表示所有F01都是免费的(或者没有F01代码)任何其他数字都是免费的F01。

A value of 0 indicates all F01 are FREE (or there are no F01 codes) any other number is a NOT FREE F01.


这篇关于如果特定列的记录在数据库中相同,如何返回true(1)或false(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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