0

Possible Duplicate:
MySQL: Select only unique values from a column

iam getting values from mysql table category and column parent. Parent contain values like 1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4

I want to skip the values if they are repeating. So i want the output as 1,2,3,4

Please help me..my code is not woking.

$new_cat1 = $cat1;
    foreach($cat1 as $category){
        $query="SELECT parent FROM categories where id='$category'";
        $result = mysql_query($query);  
        $line = mysql_fetch_assoc($result);
        array_push($new_cat1,$line['parent']);
        if (in_array("1", $new_cat1)) {
        continue;
        }
    }
Community
  • 1
  • 1
django
  • 3
  • 1
  • 2

4 Answers4

7

add DISTINCT in your query so it will select only the unique values.

SELECT DISTINCT parent FROM categories where id='$category'
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

There is also a parallel way of doing this :

SELECT DISTINCT parent FROM categories where id='$category' group by parent
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
0

Change query to that : SELECT parent FROM categories where id='$category' GROUP BY parent

Regards.

Cyril ALFARO
  • 1,616
  • 17
  • 35
0
SELECT parent FROM categories where id='$category'
group by parent 
AnandPhadke
  • 13,160
  • 5
  • 26
  • 33