Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - select where condition is true but delete if there is another row that breaks condition

Tags:

sql

mysql

I searched hours on the internet but I found nothing that would help me. Here is my problem...

My table

/ 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 

Result i want

/ id / number / idobject /
/ 7  /   90   /    3     /

I want only idobjects with at least 1 idobject true(>=50) and no idobject false

like image 850
Raold Avatar asked Dec 29 '25 20:12

Raold


1 Answers

If i get it,you want idojects with at least 1 idobject true(>=50) and no 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

EDIT

if you have more than 1 idobject true,you can replace MAX with GROUP_CONCAT

like image 113
Mihai Avatar answered Dec 31 '25 13:12

Mihai