[英]SQL - select where condition is true but delete if there is another row that breaks condition
我在互联网上搜索了几个小时,但我发现什么都没有帮助我。 这是我的问题......
我的桌子
/ id / number / idobject /
/ 1 / 50 / 2 /
/ 2 / 60 / 2 /
/ 3 / 70 / 2 /
/ 4 / 80 / 1 /
/ 5 / 10 / 2 /
/ 6 / 20 / 1 /
/ 7 / 90 / 3 /
SQL
SELECT *
FROM table
WHERE number > 50
我想要的结果
/ id / number / idobject /
/ 7 / 90 / 3 /
我只想要idobjects至少有1个idobject为true(> = 50)且没有idobject为false
如果我得到它,你想要idojects至少有1个idobject true(> = 50)并且没有idobject false
SELECT MAX(id) as id ,MAX(number) as number,idobject
FROM t
GROUP BY idobject
HAVING SUM(number <50)=0
AND SUM(number >=50)>0
编辑
如果你有超过1个idobject为true,你可以用GROUP_CONCAT替换MAX
您可以使用NOT IN子句。
select idobject from table where idobject not in(select idobject from table where number<50);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.