Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql - delete rows if null exist more than a specific num

Tags:

mysql

I have a table like :

id | name1 | name2 | name3
 1 |  asa  |  NULL |  das
 2 |  NULL |  NULL |  asas

I want to delete every row that has two or more time the NULL value (here, the one with id = 2)
I already did that with a small PHP script but i wonder if that can be done with a mysql query

I am new to mysql so i didn't try anything yet!

like image 481
Manos Serifios Avatar asked Mar 28 '13 19:03

Manos Serifios


2 Answers

You will want to use a WHERE clause with multiple filters, each one checking is the column is null:

delete 
from yourtable
where 
  (name1 is null and name2 is null) or
  (name1 is null and name3 is null) or
  (name2 is null and name3 is null) or
  (name1 is null and name2 is null and name3 is null) 

See SQL Fiddle with Demo

like image 168
Taryn Avatar answered Sep 21 '22 10:09

Taryn


delete from table where 
     (name1 is null AND name2 is null) OR
     (name2 is null AND name3 is null) OR
     (name1 is null AND name3 is null) OR
     (name1 is null AND name2 is null AND name3 is null)
like image 24
ajspacemanspiff Avatar answered Sep 18 '22 10:09

ajspacemanspiff