目前,我有一个表(2列ColumnName,Value),其数据如下:
ColumnName Value
CustomerName Facebook
CompanyName Google
如何使用And / Or条件编写查询以满足请求:
我不知道要开始。
请告知。
感谢。
答案 0 :(得分:1)
您可以研究 EAV数据模型,了解此模型可能无法很好地扩展的原因。
您可以这样查询:
declare @YourTable table (ColumnName varchar(100), Value varchar(100) primary key (ColumnName, Value));
insert into @YourTable
select 'CustomerName', 'Facebook' union all
select 'CompanyName', 'Google';
--with And...
select *
from @YourTable
where (ColumnName = 'CustomerName' and Value = 'Yahoo') and
(ColumnName = 'CompanyName' and Value = 'Google')
--with Or...
select *
from @YourTable
where (ColumnName = 'CustomerName' and Value = 'Facebook') or
(ColumnName = 'CompanyName' and Value = 'Google')