简体   繁体   English

如何检查特定属性的元素是否存在于列中?

[英]How to check if the element of a particular attribute exists in a column?

I have been given a question, where I have to Write a TSQL function that will check for the presence of department number in department table. 给了我一个问题,我必须在其中编写一个TSQL函数,该函数将检查部门表中部门编号的存在。

Here are the records present in the DEPARTMENT table:- 以下是DEPARTMENT表中存在的记录:-

DINT        DNAME                MGRSSN      MGRSTARTDATE TOTAL_SALARY
----------- -------------------- ----------- ------------ ------------
1           Headquarters         888665555   1981-06-19   55000
4           Administration       987654321   1995-01-01   98000
5           Research             333445555   1988-05-22   136000

So, if I pass 1 as a parameter, it should return "YES" because DINT = 1 exists in the table and if I pass 9 as a parameter, it should return "NO". 因此,如果我通过1作为参数,则应该返回“ YES”,因为表中存在DINT = 1;如果我通过9作为参数,则应该返回“ NO”。

I don't really know how to approach this question. 我真的不知道如何解决这个问题。 I tried figuring out how to use BIT in SQL SERVER but did not arrive at anything. 我试图弄清楚如何在SQL SERVER中使用BIT,但没有任何结果。 The function should consist of one parameter, that is the DEPARTMENT NUMBER which can be denoted as DNO. 该功能应包含一个参数,即部门号,可以将其表示为DNO。

I just want this function to return "YES" if the department number is present in the department table, else return "NO". 如果部门表中存在部门编号,我只希望此函数返回“ YES”,否则返回“ NO”。

SQL DEMO SQL演示

CREATE FUNCTION existDept
 (
 @dno AS integer
 )
RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @return_value VARCHAR(MAX);
    SET @return_value = 'NO';

    SELECT @return_value = 'YES'
    FROM dept
    WHERE department_id = @dno;

    RETURN @return_value;
END;

TEST 测试

SELECT dbo.existDept(1);
SELECT dbo.existDept(7);

In one SQL query: 在一个SQL查询中:

select IIF((select count(*) FROM DEPARTMENT where DINT=@yourParam) > 0, 'Yes', 'No') as Result

You need to replace @yourParam with the correct parameter. 您需要使用正确的参数替换@yourParam

Please try with this. 请尝试这个。

Declare @dint as int
set @dint='1'
SELECT case when count(DINT)>0 then 'Yes' else 'No' end FROM DEPARTMENT 
where DINT=@dint;

row count https://www.php.net/manual/en/pdostatement.rowcount.php 行数https://www.php.net/manual/zh/pdostatement.rowcount.php

example: 例:

<?php
$sql = "SELECT COUNT(*) FROM fruit WHERE calories > 100";
if ($res = $conn->query($sql)) {

    /* Check the number of rows that match the SELECT statement */
    if ($res->fetchColumn() > 0) {

        /* Issue the real SELECT statement and work with the results */
        $sql = "SELECT name FROM fruit WHERE calories > 100";

        foreach ($conn->query($sql) as $row) {
            print "Name: " .  $row['NAME'] . "\n";
        }
    }
    /* No rows matched -- do something else */
    else {
        print "No rows matched the query.";
    }
}

$res = null;
$conn = null;
?>

It's just a example, but it can be done with multiple ways, post your code so it will be better implemented 这只是一个示例,但是可以通过多种方式完成,发布代码,以便更好地实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM