簡體   English   中英

SQL - 選擇條件為true的位置,但如果有另一行違反條件則刪除

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM