0

i have a song name in db, while updating, there are more duplicated rows in db

for example :-

ID - 200  | TAGS - akon
ID - 201  | TAGS - akon
ID - 204  | TAGS - akon

i need to omit duplicate data's or delete those duplicate rows.

i am retrieving data usings

<? 
$result = mysql_query("SELECT * FROM tags  WHERE tag !='' ORDER by id DESC LIMIT 30");
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> :: "; 
}
?>

i need to omit or delete those duplicated rows and show only unique content

Andomar
  • 232,371
  • 49
  • 380
  • 404
azarudeen
  • 125
  • 3
  • 16

1 Answers1

1

You can use DISTINCT to select non duplicated tags:

SELECT 
  DISTINCT tag
FROM tags  
WHERE tag !='' 
ORDER by id DESC 
LIMIT 30
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
  • how to delete those duplicate rows from db – azarudeen Aug 30 '14 at 18:03
  • 1
    +1 Distinct doesn't require or use parenthesis. Luckily, `(col1)` is the same as `col1`. And the SQL standard not equal operator is `<>`, not `!=`, though both work in MySQL. – Andomar Aug 30 '14 at 18:05
  • @user3663603: See http://stackoverflow.com/questions/3311903/remove-duplicate-rows-in-mysql – Andomar Aug 30 '14 at 18:05
  • @Andomar thanks, parenthesis it's a habit I have that sometime I forget in mysql. If you want to delete duplicated rows there's plenty of posts, look [here](http://stackoverflow.com/questions/4685173/delete-all-duplicate-rows-except-for-one-in-mysql) or [here](http://stackoverflow.com/questions/3311903/remove-duplicate-rows-in-mysql). – Ende Neu Aug 30 '14 at 18:07