这是一种挑战。预期输出,所有水果都来自纽约州或 ak 州,如果标记好 = 价格 10 和坏 = 价格 20 和 OK = 价格 40。所以,来自纽约州的苹果,标记好和坏匹配价格 10 和 20,和苹果没有标记OK行,所以不需要比较。 Banana mark bad = price 20, but OK is not equal price 40, 所以我会排除香蕉。 pear match OK = price 40 and state=ny 并且没有好坏标记行。因此,我的输出应该是苹果和梨。我应该怎么做?
<?php
/*
fruit state price mark
apple ny 10 good
apple ny 20 bad
banana ny 20 bad
banana ny 30 OK
pear ny 40 OK
berry pa 10 good
*/
/*this is my best query but fail to do what I expected*/
SELECT fruit FROM table WHERE state IN ('ny','ak') AND mark IN ('good', 'bad', 'OK') AND price IN ( '10','20','40') GROUP BY fruit
?>
最佳答案
你应该在哪里和或条件
你应该使用一对 not in
select distinct fruit from my_table
where fruit not in (select distinct fruit from my_table
where (mark, price) not in (select 'good', 10 from dual
union
select 'bad, 20 from dual
union
select 'OK', 40 from dual))
state IN ('ny','ak') ;
关于php - 基于此表,如何获得预期的输出 mysql php?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38622435/