我有两张桌子
KeyWord Table
| KeyWord1 | Operator | KeyWord2 |
| -----------|------------:|:------------:|
| Tom | AND | Harry |
| Krishna | OR | Radha |
| Raju | AND | Radha |
Text Table
| Id | Text |
| -----------|-------------------------------:|
| 1 | Tom and Harry Nice |
| 2 | Tom Harry Raju Radha Nice|
| 3 | Raju Radha Tom Nice |
预期结果是
结果
| Id | Text |
| -----------|-------------------------------:|
| 2 | Tom Harry Raju Radha Nice|
结果是因为(Tom AND Harry) AND (Krishna OR Radha) AND (Raju AND Radha)
。我需要使用LIKE
运算符(LIKE '%'+tbl.Text+'%'
)来获取布尔表达式。
如果没有非动态sql方法,请告诉我最好的动态sql方法。
答案 0 :(得分:1)
以下工作没有动态SQL或游标/ while循环。关于它的几点说明:
NOT
EXISTS
,那么单个错误条件会给出与所有正确条件相同的答案。
OR
条件不是一个好的测试,因为它具有一个AND
条件集(即Radha
)所需的值,因此不需要OR
设置,无法确定逻辑是否适用于AND
和OR
条件。所以:
Radha
更改为Bob
Krishna
的第4行,这是唯一应该(并且确实!)匹配的行代码:
DECLARE @TheText TABLE (ID INT, TheText NVARCHAR(500));
INSERT INTO @TheText VALUES (1, N'Tom and Harry Nice');
INSERT INTO @TheText VALUES (2, N'Tom Harry Raju Radha Nice');
INSERT INTO @TheText VALUES (3, N'Raju Radha Tom Nice');
INSERT INTO @TheText VALUES (4, N'Tom Harry Raju Radha Krishna Nice');
DECLARE @Keywords TABLE (
Keyword1 NVARCHAR(100) NOT NULL,
Operator NVARCHAR(10) NOT NULL,
Keyword2 NVARCHAR(100) NOT NULL
);
INSERT INTO @Keywords VALUES ('Tom', 'AND', 'Harry');
INSERT INTO @Keywords VALUES ('Krishna', 'OR', 'Bob');
INSERT INTO @Keywords VALUES ('Raju', 'AND', 'Radha');
SELECT *
FROM @TheText txt
WHERE NOT EXISTS (
SELECT *
FROM @Keywords kw
WHERE (
kw.Operator = N'AND'
AND (
txt.TheText NOT LIKE N'%' + kw.Keyword1 + N'%'
OR txt.TheText NOT LIKE N'%' + kw.Keyword2 + N'%'
)
)
OR (
kw.Operator = N'OR'
AND (
txt.TheText NOT LIKE N'%' + kw.Keyword1 + N'%'
AND txt.TheText NOT LIKE N'%' + kw.Keyword2 + N'%'
)
)
);
结果:
ID TheText
4 Tom Harry Raju Radha Krishna Nice
答案 1 :(得分:0)
我不确定,没有动态SQL还有其他方法可做。 尝试下面的代码,这肯定会帮助你。我在sybase中尝试过它很棒!
--Create Sample Tables
--Keyword Table
create table #tempKeyword
(col_id numeric(5,0) identity , Keyword1 varchar(100),Operator varchar(100),Keyword2 varchar(100))
insert into #tempKeyword (Keyword1,Operator,Keyword2)
select *
from
(
select 'Tom' as Keyword1, 'AND' as Operator, 'Harry' as Keyword2
union all
select 'Krishna' as Keyword1, 'OR' as Operator, 'Radha' as Keyword2
union all
select 'Raju' as Keyword1, 'AND' as Operator, 'Radha' as Keyword2
) a
print 'Keyword Table'
select * from #tempKeyword
--Text table
select * into #tempText from
(
select 1 as Id, 'Tom and Harry Nice' as Text
union all
select 2 as Id, 'Tom Harry Raju Radha Nice' as Text
union all
select 3 as Id, 'Raju Radha Tom Nice' as Text
) b
print 'Text Table'
select * from #tempText
SET NOCOUNT ON
---- Script begin
declare @Keyword1 varchar(100)
declare @Operator varchar(100)
declare @Keyword2 varchar(100)
declare @sql varchar(1000)
--settin loop counter variables
declare @start int
declare @end int
select @start=min(col_id),@end=max(col_id) from #tempKeyword
--loop for #temp table
WHILE (@start <= @end)
BEGIN
select top 1 @Keyword1=Keyword1,@Operator=Operator,@Keyword2=Keyword2
from #tempKeyword where col_id=@start
--eliminate each rows with NOT condition from result table.
select @sql='delete from #tempText where not ( Text like ''%' + @Keyword1 +
'%''' + @Operator +' Text like ''%' + @Keyword2 + '%'')'
execute(@sql)
--set Counter for Loop
set @start = @start + 1
end
SET rowcount 0
--Result Output
print 'Results'
select * from #tempText
&#13;
我希望这会有所帮助:)