1

タイトルがわかりにくい場合は申し訳ありませんが、例で説明したほうがよいでしょう。

私はこのようなデータのテーブルを持っています:

1    ProductA    'Cheesy'
2    ProductA    'Creamy'
3    ProductA    'Juicy'
4    ProductB    'Buttery'
5    ProductB    'Clean'
6    ProductC    'Bitter'

「Cheesy」や「Juicy」などの検索語を貼り付けたい。これは戻るはずです:

ProductA

...ProductAがID1および3と一致するため。

しかし、「Cheesy」と「Bitter」を検索すると、ProductAには「Cheesy」が含まれている可能性がありますが、「Bitter」のレコードが含まれていないため、レコードは返されません。

これは可能ですか?

4

2 に答える 2

2

1つのアプローチ:

declare @Products as Table ( ProductId Int Identity, Product VarChar(16), Property VarChar(16) )
insert into @Products ( Product, Property ) values
  ( 'ProductA', 'Cheesy' ), ( 'ProductA', 'Creamy' ), ( 'ProductA', 'Juicy' ),
  ( 'ProductB', 'Buttery' ), ( 'ProductB', 'Clean' ),
  ( 'ProductC', 'Bitter' )

select Product
  from @Products
  where Property = 'Cheesy'
intersect
select Product
  from @Products
  where Property = 'Juicy'

編集:追加の例:

-- To retrieve all data for the matching product(s):    
select *
  from @Products
  where Product in (
    select Product
      from @Products
      where Property = 'Cheesy'
    intersect
    select Product
      from @Products
      where Property = 'Juicy' )
于 2012-04-12T16:39:31.420 に答える
1
select product from products 
where property = 'Cheesy' -- property 1
or 
property = 'Juicy' -- property 2
group by product
having count(*) >= 2 -- number of properties

これらの線に沿った何かもうまくいくと思います。

于 2012-04-12T16:52:15.163 に答える