Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete blank rows in Mysql?

I do have a table with more than 100000 data elements, but there are almost 350 blank rows within. How do I delete this blank rows using phpmyadmin? Manually deleting is a tedious task.

like image 270
Jose H d Avatar asked Jan 08 '11 09:01

Jose H d


3 Answers

The general answer is:

DELETE FROM table_name WHERE some_column = '';

or

DELETE FROM table_name WHERE some_column IS NULL;

See: http://dev.mysql.com/doc/refman/5.0/en/delete.html

More info when you post your tables!~

Also, be sure to do:

SELECT * FROM table_name WHERE some_column = '';

before you delete, so you can see which rows you are deleting! I think in phpMyAdmin you can even just do the select and then "select all" and delete, but I'm not sure. This would be pretty fast, and very safe.

like image 133
Spiny Norman Avatar answered Oct 17 '22 10:10

Spiny Norman


I am doing the mysql operation in command prompt in windows. And the basic queries:

delete * from table_name where column=''

and

delete * from table_name where column='NULL'

doesn't work. I don't know whether it works in phpmyadmin sqlcommand builder. Anyway:

delete * from table_name where column is NULL 

works fine.

like image 5
NCA Avatar answered Oct 17 '22 09:10

NCA


I have a PHP script that automatically removes empty rows based on column data types.

That allows me to define "emptiness" differently for different column types.

e.g.

table
first_name (varchar) | last_name (varchar) | some_qty ( int ) | other_qty (decimal)

DELETE FROM `table` WHERE
(`first_name` IS NULL OR `first_name` = '')
AND
(`last_name` IS NULL OR `last_name` = '')
AND
(`some_qty` IS NULL OR `some_qty` = 0)
AND
(`other_qty` IS NULL OR `other_qty` = 0)

Since "0" values are meaningless in my system, I count them as empty. But I found out that if you do (first_name = 0) then you will always get true, because strings always == 0 in MySQL. So I tailor the definition of "empty" to the data type.

like image 3
Buttle Butkus Avatar answered Oct 17 '22 11:10

Buttle Butkus