所以我有这样的表格:
|---group---|---property---|---value---|
| a | prop1 | yes |
| a | prop2 | yes |
| a | prop3 | no |
| b | prop1 | yes |
| b | prop2 | no |
| b | prop3 | no |
| c | prop1 | no |
| c | prop2 | no |
| c | prop3 | no |
我只需要过滤掉那些yes
接近prop1
的组。 我可以使用having
语句,但这需要任何yes
值。问题是我没有将我的属性作为列名,而是作为列中的行。所以。。。甚至有可能做到这一点吗?
在我的结果表中应该只有a
和b
组......
您可以使用子查询首先选择值为Property
= 'prop1' 且Value
= 'Yes' 的组列表。然后为这些组列表选择所有记录,如下所示-
SELECT *
FROM your_table
WHERE Group IN(
SELECT DISTINCT `Group`
FROM your_table
WHERE `Property` = 'prop1'
AND `Value` = 'Yes'
)
您可以添加几个不同的属性,如-
....
WHERE
(
`Property` = 'prop1'
AND `Value` = 'Yes'
)
AND -- You can also use OR here if you need rows if any of the condition is true
(
`Property` = 'prop3'
AND `Value` = 'No'
)
单个属性很简单
select distinct group
from myTable
where property = 'prop1' and value ='yes';
您可以使用 EXISTS:
select t.* from tablename t
where exists (
select 1 from tablename
where `group` = t.`group`
and (
(property = 'prop1' and `value` = 'yes')
or
(property = 'prop2' and `value` = 'no')
)
)
您可以在子查询中组合条件。
仅供参考,您也可以编写此内容(尽管它使用索引的能力可能更有限...
SELECT * FROM my_table WHERE(x,y) IN((1,'yes'),(2,'no'),(3,'maybe'));