sql 服务器匹配 2 table 列进行完全匹配
sql server matching 2 table columns for an exact word match
我想在 table 的 varchar 列中搜索另一个 table 的 varchar 列中的内容。
某些词被禁止,我想确定包含被禁止词的行。我想要一个完全匹配的禁用词。
我正在使用 MS SQL Server 2016。
Table 1:
CREATE TABLE [dbo].[BlogComment](
[BlogCommentId] [int] IDENTITY(1,1) NOT NULL,
[BlogCommentContent] [varchar](max) NOT NULL,
CONSTRAINT [PK_BlogComment] PRIMARY KEY CLUSTERED
(
[BlogCommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
3 行 - BlogCommentContent 中的数据:
我们有很多人。
就是这个人。
我听到了。
Table 2:
CREATE TABLE [dbo].[BannedWords](
[BannedWordsId] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](250) NOT NULL
CONSTRAINT [PK_BannedWords] PRIMARY KEY CLUSTERED
(
[BannedWordsId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
3 行 - 以及描述中的数据:
虽然
男人
听到
我的Sql:
SELECT BlogCommentContent
FROM dbo.BlogComment,
dbo.BannedWords
WHERE ( CHARINDEX( [Description], BlogCommentContent, 1 ) ) > 1
在单词 'many' 中查找 'man'、'hear' 和 'man'。
所以它 returns 3 行。
我只想要完全匹配。
所以只有 return 2 行。
我该如何完成?
如果你想要完全匹配,你的意思是不能有其他作品触及,所以男人可以匹配“男人”、“男人和女人”、“老鼠和男人”、“一两个男人”。你需要检查
BlogCommentContent = 'man'
left(BlogCommentContent,3) = 'man'
right(BlogCommentContent,3) = 'man'
BlogCommentContent like ' man '
man的长度可以在len('man')
中找到,用在right()
和left()
中。
like
的最后一个值可以用 concat(' ','man',' ')
构造
请尝试以下解决方案。
它将从 SQL Server 2017 开始运行。
SQL
-- DDL and sample data population, start
DECLARE @BlogComment TABLE (
BlogCommentId INT IDENTITY PRIMARY KEY,
BlogCommentContent VARCHAR(MAX));
INSERT INTO @BlogComment (BlogCommentContent) VALUES
('There are many of us.'),
('This is the man.'),
('I hear you.');
DECLARE @BannedWords TABLE (
BannedWordsId INT IDENTITY PRIMARY KEY,
Word varchar(250))
INSERT INTO @BannedWords (Word) VALUES
('though'),
('man'),
('hear');
-- DDL and sample data population, end
;WITH rs AS
(
SELECT word = TRIM('.,' FROM value )
FROM @BlogComment
CROSS APPLY STRING_SPLIT(BlogCommentContent, SPACE(1))
)
SELECT DISTINCT bw.Word
FROM rs
INNER JOIN @BannedWords bw ON rs.word = bw.Word;
SQL 服务器 2016
;WITH rs AS
(
--SELECT word = TRIM('.,' FROM [value])
SELECT word = REPLACE(REPLACE([value],'.',''),',','')
FROM @BlogComment
CROSS APPLY STRING_SPLIT(BlogCommentContent, SPACE(1))
)
SELECT DISTINCT bw.Word
FROM rs
INNER JOIN @BannedWords bw ON rs.word = bw.Word;
输出
+------+
| Word |
+------+
| man |
| hear |
+------+
我想在 table 的 varchar 列中搜索另一个 table 的 varchar 列中的内容。
某些词被禁止,我想确定包含被禁止词的行。我想要一个完全匹配的禁用词。
我正在使用 MS SQL Server 2016。
Table 1:
CREATE TABLE [dbo].[BlogComment](
[BlogCommentId] [int] IDENTITY(1,1) NOT NULL,
[BlogCommentContent] [varchar](max) NOT NULL,
CONSTRAINT [PK_BlogComment] PRIMARY KEY CLUSTERED
(
[BlogCommentId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
3 行 - BlogCommentContent 中的数据:
我们有很多人。
就是这个人。
我听到了。
Table 2:
CREATE TABLE [dbo].[BannedWords](
[BannedWordsId] [int] IDENTITY(1,1) NOT NULL,
[Description] [varchar](250) NOT NULL
CONSTRAINT [PK_BannedWords] PRIMARY KEY CLUSTERED
(
[BannedWordsId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
3 行 - 以及描述中的数据:
虽然
男人
听到
我的Sql:
SELECT BlogCommentContent
FROM dbo.BlogComment,
dbo.BannedWords
WHERE ( CHARINDEX( [Description], BlogCommentContent, 1 ) ) > 1
在单词 'many' 中查找 'man'、'hear' 和 'man'。 所以它 returns 3 行。
我只想要完全匹配。 所以只有 return 2 行。
我该如何完成?
如果你想要完全匹配,你的意思是不能有其他作品触及,所以男人可以匹配“男人”、“男人和女人”、“老鼠和男人”、“一两个男人”。你需要检查
BlogCommentContent = 'man'
left(BlogCommentContent,3) = 'man'
right(BlogCommentContent,3) = 'man'
BlogCommentContent like ' man '
man的长度可以在len('man')
中找到,用在right()
和left()
中。
like
的最后一个值可以用 concat(' ','man',' ')
请尝试以下解决方案。
它将从 SQL Server 2017 开始运行。
SQL
-- DDL and sample data population, start
DECLARE @BlogComment TABLE (
BlogCommentId INT IDENTITY PRIMARY KEY,
BlogCommentContent VARCHAR(MAX));
INSERT INTO @BlogComment (BlogCommentContent) VALUES
('There are many of us.'),
('This is the man.'),
('I hear you.');
DECLARE @BannedWords TABLE (
BannedWordsId INT IDENTITY PRIMARY KEY,
Word varchar(250))
INSERT INTO @BannedWords (Word) VALUES
('though'),
('man'),
('hear');
-- DDL and sample data population, end
;WITH rs AS
(
SELECT word = TRIM('.,' FROM value )
FROM @BlogComment
CROSS APPLY STRING_SPLIT(BlogCommentContent, SPACE(1))
)
SELECT DISTINCT bw.Word
FROM rs
INNER JOIN @BannedWords bw ON rs.word = bw.Word;
SQL 服务器 2016
;WITH rs AS
(
--SELECT word = TRIM('.,' FROM [value])
SELECT word = REPLACE(REPLACE([value],'.',''),',','')
FROM @BlogComment
CROSS APPLY STRING_SPLIT(BlogCommentContent, SPACE(1))
)
SELECT DISTINCT bw.Word
FROM rs
INNER JOIN @BannedWords bw ON rs.word = bw.Word;
输出
+------+
| Word |
+------+
| man |
| hear |
+------+