我需要在sql查询中使用like运算符来检查数据库记录中另一个文本中的文本单词,以查找类似该文本单词的任何记录 例如:
my text is : ajax,net,apache,sql
and records like :
assembly,c#,java,apache
ajax,pascal,c,c++
...
我需要查询以查找任何包含我的文字的行
此图片是搜索输入
我将在搜索字符串中找到任何包含单词的行
SELECT
dbo.tblProjects.id, dbo.tblProjects.prTitle, dbo.tblUsers.id AS UserID,tblUsers.nickname,
dbo.tblProjects.prTags, dbo.tblProjects.prDesc, dbo.tblProjects.prFaDate
FROM
dbo.tblUsers
INNER JOIN
dbo.tblProjects ON dbo.tblUsers.id = dbo.tblProjects.UserID
where (tblUsers.id=@userid)and(dbo.tblProjects.tags like @userskills + '%')
order by dbo.tblProjects.id desc
萨米语更新
ALTER procedure [dbo].[Dashboard_My_Skills_Projects]
(
@userid bigint,
@userskills nvarchar(100)
)
as
begin
SELECT T.id,
T.prTitle,
U.id AS [UserID],
U.nickname,
T.prTags,
T.prDesc,
T.prFaDate
FROM dbo.tblUsers U INNER JOIN dbo.tblProjects T ON U.id = T.UserID
WHERE U.id = @userid
AND
T.tags IN(
SELECT tags
FROM dbo.tblProjects T1 CROSS JOIN
(
SELECT Value
FROM STRING_SPLIT(@userskills, ',')
) TT
WHERE T1.tags LIKE '%' + Value + '%'
)
order by T.id desc
end
tblUsers
tblProjects
答案 0 :(得分:1)
您可以将CROSS APPLY
,STRING_SPLIT()
和LIKE
用作
CREATE TABLE T(
Tags VARCHAR(100)
);
INSERT INTO T VALUES
('Analytics,Amazon Web Service,Active Directory'),
('BMC Remedy,Big Data,Ajax'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Azure,Assembly,Appache'),
('Apple Safari,Analytics,Ajax');
DECLARE @Search VARCHAR(45) = 'Active Directory,Ajax,Azure';
SELECT DISTINCT Tags
FROM T CROSS APPLY (SELECT Value FROM STRING_SPLIT(@Search, ',')) TT
WHERE Tags LIKE '%' + Value + '%';
返回:
+-------------------------------------------------+
| Tags |
+-------------------------------------------------+
| Analytics, Amazon Web Service, Active Directory |
| Apple Safari, Analytics, Ajax |
| Azure, Assembly, Appache |
| BMC Remedy, Big Data, Ajax |
+-------------------------------------------------+
注意: STRING_SPLIT()
功能仅在2016+版本上可用,因此如果您不使用2016+版本,则需要创建自己的功能。
更新
CREATE TABLE Tags(
Tags VARCHAR(100),
UserID INT
);
CREATE TABLE Users(
UserID INT,
UserName VARCHAR(45)
);
INSERT INTO Tags VALUES
('Analytics,Amazon Web Service,Active Directory', 1),
('BMC Remedy,Big Data,Ajax', 2),
('Azure,Assembly,Appache', 3),
('Azure,Assembly,Appache', 1),
('Azure,Assembly,Appache', 4),
('Azure,Assembly,Appache', 2),
('Apple Safari,Analytics,Ajax', 1);
INSERT INTO Users VALUES
(1, 'User1'),
(2, 'User2'),
(3, 'User3'),
(4, 'User4');
DECLARE @Search VARCHAR(45) = 'Active Directory,Azure',
@UserID INT = 1;
SELECT U.UserID,
U.UserName,
T.Tags Skills
FROM Users U INNER JOIN Tags T ON U.UserID = T.UserID
WHERE U.UserID = @UserID
AND
T.Tags IN(
SELECT Tags
FROM Tags T1 CROSS JOIN
(
SELECT Value
FROM STRING_SPLIT(@Search, ',')
) TT
WHERE T1.Tags LIKE '%' + Value + '%'
)
和 here is your stored procedure works just fine and as expected
答案 1 :(得分:0)
Like
将在列中搜索指定的模式。
并在评论中注明@Pedro。.我希望您不要只在1条记录中做到这一点。
您可以使用
SELECT *
FROM your_table
WHERE your_text LIKE '%ac%';
您可以在Here中了解有关Like
运算符的更多信息
编辑:
您可以使用String_Split
功能
SELECT
*
FROM
table
WHERE
EXISTS (SELECT
*
FROM
STRING_SPLIT(Tags, ',')
WHERE
value IN ('Active Directory', 'Ajax', 'Azure')
答案 2 :(得分:0)
Create FUNCTION fn_SplitADelimitedList
(
@String NVARCHAR(MAX)
)
RETURNS @SplittedValues TABLE(
Value nvarchar(500)
)
As
BEGIN
DECLARE @SplitLength INT
DECLARE @Delimiter VARCHAR(10)
SET @Delimiter = ','
WHILE len(@String) > 0
BEGIN
SELECT @SplitLength = (CASE charindex(@Delimiter, @String)
WHEN 0 THEN
datalength(@String) / 2
ELSE
charindex(@Delimiter, @String) - 1
END)
INSERT INTO @SplittedValues
SELECT cast(substring(@String, 1, @SplitLength) AS nvarchar(50))
WHERE
ltrim(rtrim(isnull(substring(@String, 1, @SplitLength), ''))) <> '';
SELECT @String = (CASE ((datalength(@String) / 2) - @SplitLength)
WHEN 0 THEN
ELSE
right(@String, (datalength(@String) / 2) - @SplitLength - 1)
END)
END
RETURN
END
DECLARE @yourtext NVARCHAR(400) = 'Apple, Active Directory'
SELECT
LTRIM(RTRIM(c.Value)),
em.tags
FROM tblTest em
CROSS APPLY fn_SplitADelimitedList(em.tags) c
inner join
(SELECT Split.a.value('.', 'NVARCHAR(MAX)') DATA
FROM
(
SELECT CAST('<X>'+REPLACE(@yourtext, ',', '</X><X>')+'</X>' AS XML) AS String
) AS A
CROSS APPLY String.nodes('/X') AS Split(a)) v
on LTRIM(RTRIM(c.Value)) = LTRIM(RTRIM(v.DATA))