SELECT DISTINCT语句显示重复值

时间:2016-03-23 11:25:01

标签: php html

我有这个功能只从db

中获取不同的值
function get_all_product_data_admin()
{
  $query = "SELECT DISTINCT category FROM products ORDER BY product_id AS";
  mysql_query("SET NAMES 'utf8'");
  $result = mysql_query($query);
  if($result == false) //Something is wrong with the query
    echo "result non";
  if(mysql_num_rows($result) == 0) //No Pages
    echo "result 0";

  while($row = mysql_fetch_assoc($result)) // MAKES 2D ARRAY FOR DRAWING A LIST
  {
    $returned_array[] = $row;        
  }
  return $returned_array;
}

这是html。我使用switch,因此英文值将显示为希伯来语翻译。

$menu = get_all_product_data_admin(); 
foreach($menu as $key=>$val) { 
    switch($val['category'])
    {
        case "bread": $catt = "לחמים"; break;
        case "buns": $catt = "לחמניות"; break;
        case "bake": $catt = "מאפים"; break;
        case "cake": $catt = "עוגות"; break;
        case "cookies": $catt = "עוגיות"; break;
        case "dessert": $catt= "קינוחים אישיים"; break;
        case "pita": $catt = "פיתות"; break;
        case "friday": $catt = "חלות יום ו"; break;
        case "baget": $catt = "באגטים"; break;
        case "jabeta": $catt = "ג'בטות"; break;
        case "nogloten": $catt = "ללא גלוטן"; break;
        case "holiday": $catt = "מוצרי חגים"; break;
        case "special": $catt = "מיוחדים"; break;
    }
    echo '
    <a style="float:right;" href="add_product.php?cname='.$val['category'].'">
        '.$catt.'
    </a>
    ';
}

我仍然有重复的价值观。任何人?

1 个答案:

答案 0 :(得分:2)

使用Group by

SELECT category FROM products Group by category

GROUP BY语句与聚合函数结合使用,可以按一列或多列对结果集进行分组。