azarudeen azarudeen - 4 years ago 122
SQL Question

How to delete empty rows in SQL while getting data from database?

I need to get last 50 tags from database, there will be more empty rows in database, I need to delete that empty rows, while getting data from database. Is there any way to delete empty rows in database?

<?
$result = mysql_query("SELECT * FROM tags ORDER by id DESC LIMIT 50");
while($row = mysql_fetch_array($result)) {
$title = str_replace('-',' ',$row['tag']);
if (strlen($title) > 50)
$title = substr($title, 0, strrpos(substr($title, 0, 50), ' ')) . '...';
$title = str_replace('---','-',$title);
$title = str_replace('--','-',$title);
$tag = str_replace('---','-',$row['tag']);
$tag = str_replace('--','-',$tag);
echo "<a href=/mp3/".UrlText($tag)."/ title=\"".$title."\">".$title . "</a> :: ";
}
?>

Answer Source

To physically remove rows with empty (NULL) tag field use this query:

DELETE FROM tags WHERE tag IS NULL OR tag = '';

If you want to keep this rows but don't want to include it to query set use this query:

SELECT * 
FROM tags 
WHERE tag IS NOT NULL AND tag != ''
ORDER BY id DESC LIMIT 30
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download