简体   繁体   English

搜索SQL并返回true或false

[英]Search SQL and return true or false

I have a table that has thousands of rows in. I need to check if certain values exists in the table or not. 我有一个包含数千行的表。我需要检查表中是否存在某些值。

I want to list all the bar codes I am searching with a flag of true or false returned if there is one. 我想列出所有我正在搜索的条形码,如果有则返回一个true或false标志。

I have come up with this so far: 到目前为止,我已经提出了:

SELECT CASE WHEN EXISTS (
    SELECT *
    FROM TABLE
    WHERE Coulmn in ('a','b', 'c', 'd', 'e', 'f', 'g')
)
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END 

This however just returns a value of 1. 但是,这仅返回值1。

So in the table I have 所以在桌子上我有

coulmn
----------
A
B
D
E
F
G

I want to do a search that returns the following 我想搜索返回以下内容

Coulmn | Exsists
-----------------
A      |  True
B      |  True
C      |  False
D      |  True
E      |  True
F      |  True
G      |  True

This works: 这有效:

SELECT *, CASE WHEN (Column in ('1','2')) THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END AS result_field
FROM TABLE;

NOTE: Tested in PostgreSQL 注意:在PostgreSQL中测试

You can use a query like the following: 您可以使用如下查询:

SELECT t1.v,
       CASE WHEN t2.col IS NOT NULL THEN 'true' ELSE 'false' END AS Exists
FROM (
   SELECT 'a' AS v UNION ALL SELECT 'b' UNION ALL SELECT 'c' UNION ALL SELECT 'd'
   UNION ALL SELECT 'e' UNION ALL SELECT 'f' UNION ALL SELECT 'g') AS t1
LEFT JOIN mytable AS t2 ON t1.v = t2.col

As it's written, the outer select is select case when exists () then 1 else 0 end... so it is only going to return one row. 如所写,外部选择是存在()的情况下的选择情况,然后1否则0结束...因此它只会返回一行。 The outer select must include "Column" AND "Exists" (select column, ...) to return two columns. 外部选择必须包括“列”和“存在”(选择列,...)以返回两列。

A "where" clause will never return a "false" like this, though, because "column" has to be in a real table for the query to actually return it. 但是,“ where”子句永远不会返回这样的“ false”,因为“ column”必须在实际表中才能实际返回查询。 As @jarlh says, you'll need a helper table to store the columns you're looking for: 正如@jarlh所说,您将需要一个帮助程序表来存储您要查找的列:

Create table SearchColumns (SearchColumn char(1));
insert into SearchColumns (SearchColumn)
     values ('A'), ('B'), ('C'), ('D'), ('E'), ('F'), ('G'), ('H')

Then you can do the If Exists to your table from that table to see which values are in or not in: 然后,您可以从该表中对表进行“如果存在”,以查看其中存在或不存在哪些值:

select SearchColumn, case when exists 
    (select * from TABLE where Table.Column = SearchColumns.SearchColumn)
    then 'True' else 'False' end as ExistsStatus
from SearchColumns

I think that will get you what you want. 我认为这将为您提供所需的东西。 This gets a) Only one record per column no matter how many times it occurs in your table and b) "True" and "False" for every column value you're looking for. 这样就得到了:a)每列只有一条记录,无论它在表中出现了多少次; b)对于要查找的每个列值,分别为“ True”和“ False”。 If you really wanted a Bit, you can use 0 and 1 and the casting from the original query, but they actually show "0" and "1"; 如果您确实想要位,则可以使用0和1以及原始查询中的强制类型转换,但实际上它们显示的是“ 0”和“ 1”。 and c) this should work no matter how many values you have. c)无论您拥有多少值,这都应该起作用。

(Note, I assumed some of those were spelling errors, so I made adjustments, but they were consistent so I'm not certain). (请注意,我认为其中一些是拼写错误,因此我进行了调整,但它们是一致的,因此不确定)。

With the help form above I created a temp table and then implemented one of the soultions shared. 通过上面的帮助表,我创建了一个临时表,然后实现了共享的灵魂之一。

CREATE TABLE #Temp
(
       Barcode VARCHAR (100)
)
INSERT INTO #Temp
VALUES
(1),
(2),
(3),
(4 )
select barcode, case when exists 
    (select * from CIPKORHHTProductDetails where CIPKORHHTProductDetails.Barcode = #temp.barcode)
    then 'True' else 'False' end as ExistsStatus
from #temp order by ExistsStatus DESC

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

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