简体   繁体   English

T-SQL选择记录,其中某个字段包含另一个表中的某个关键字

[英]T-SQL selecting records where a field contains a certain keyword from another table

I'm trying to run the below SQL to return records where the "a.PatientCommentText" contains a "bad keyword" as indicated by "b.Keyword" below. 我试图运行下面的SQL以返回记录,其中“ a.PatientCommentText”包含一个“不良关键字”,如下面的“ b.Keyword”所示。 I do not think this is returning everything because the below query only returns 253 records, and the inverse of this (where charindex(b.Keyword, a.PatientCommentText) = 0 returns 7,378 records, but the total count of PES_Data is 9,821. I would expect the sum of the two scenarios to equal the record count of PES_Data. What am I doing wrong? 我不认为这会返回所有内容,因为下面的查询仅返回253条记录,而与此相反(其中charindex(b.Keyword,a.PatientCommentText)= 0返回7,378条记录,但PES_Data的总数为9,821。会期望这两种情况的总和等于PES_Data的记录计数。我在做什么错?

Returns 253 records 返回253条记录

select a.* from PES_Data a
inner join Bad_Keywords b on 1=1
where charindex(b.Keyword, a.PatientCommentText) <> 0

Returns 7,378 records 返回7,378条记录

select a.* from PES_Data a
inner join Bad_Keywords b on 1=1
where charindex(b.Keyword, a.PatientCommentText) = 0

But below returns 9,821 records 但下面返回9,821条记录

select a.* from PES_Data a

You would appear to have some PatientCommentText values that are NULL . 您似乎会出现一些NULL PatientCommentText值。

Try adding this to your results: 尝试将其添加到您的结果中:

select pd.*
from PES_Data pd
where pd.PatientCommentText is null;

In addition, you may be getting duplicates, for comments that contain more than one "bad word". 此外,对于包含多个“坏词”的注释,您可能会得到重复。 I wouldn't expect the numbers to add up, unless you know there are no duplicates. 我不希望数字加起来,除非您知道没有重复项。

+1 to Gordon Linoff's answer. +1(Gordon Linoff)的答案。

DECLARE @tblA TABLE(Base VARCHAR(100));
DECLARE @tblB TABLE(Reference VARCHAR(100));

-- Total 7 rows
INSERT INTO @tblA VALUES   
('abcdaa')   
,('bcdeab') 
,('cdefbb')   
,('defgbc')  
,('efghcc')
,('fghddd')
,(NULL)

select count(*) from @tblA where CHARINDEX('aa',Base) <> 0        -- Returns 1  
select count(*) from @tblA where CHARINDEX('aa',Base) = 0         -- Returns 5
select count(*) from @tblA where CHARINDEX('aa',Base) IS NULL     -- Returns 1

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

相关问题 SQL如何从另一个表中获取包含关键字的记录 - SQL How to get the records where contains keywords from another table T-SQL遍历一个表中的记录并基于另一表修改字段值 - T-SQL to loop through records in one table and modify field value based on another table T-SQL请求。从表中选择“树枝” - T-SQL request. Selecting “tree branch” from the table T-SQL从表的不同记录中选择最小值 - T-SQL Select minimum values from different records of the table T-SQL如何从表中减去两条记录 - t-sql how to subtract two records from table BigQuery 标准 SQL SELECT 行 WHERE 字段包含来自另一个表字段的单词 - BigQuery Standard SQL SELECT rows WHERE field contains words from another table field SQL:从表中选择all(相关表),没有指定的字段值 - SQL: Selecting all from a table, where related table, doesn't have a specified field value T-SQL获取唯一键列的所有记录,该唯一键列包含另一个列值 - T-SQL Get all records of a unique key column that contains a column value both not another SQL选择行,其中字段包含来自另一个表的字段的单词 - SQL select rows where field contains word from another table's fields SQL选择记录,其中一个字段的值与另一字段的值相似 - SQL selecting records where one field's values are similar to another field's values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM