我在数据库表中存储了几个接口元素,用户可以通过输入关键字(存储在另一个表中)来过滤这些元素:
表格按钮:
ID Name
1 Button1
2 Button2
3 Button3
表格关键字:
ButtonID Keyword
1 Garden
2 House
3 Garden
3 House
如果用户输入Garden
,则数据库返回Button1 and Button3
。
如果用户输入House
,则数据库返回Button2 and Button3
。
如果用户输入Garden AND House
,则db仅返回Button3
。
最后一个是问题,我设法将这个查询放在一起:
SELECT T.ID, T.Name
, T.Type
, T.Description
, T.Action
, T.Image
FROM Tiles T
JOIN Keywords K
ON T.ID=K.TileID
WHERE K.Keyword IN ('House', 'Garden')
不幸的是,此查询返回所有三个带有提供的关键字中的任何一个的按钮。但是我只想要包含所有提供的关键字的元素是Button3
。该查询应如何实现呢?
非常感谢 :)
declare @params table (Keyword varchar(6) primary key)
insert into @params
select 'House' union all
select 'Garden'
select
b.Name
from Keywords as k
inner join Buttons as b on b.ID = k.ButtonID
where k.Keyword in (select Keyword from @params)
group by b.Name
having count(*) = (select count(*) from @params)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句