如何在MySQL中只返回一次列值

Chi*_*gns 2 php mysql database

我需要帮助.我有一个显示产品类型标题的数据库.

到目前为止我的代码是:

    $query = mysql_query("SELECT type FROM certs ORDER by type") or die("Could not execute query"); 


while($row = mysql_fetch_array($query) )
{

   $product .= "<h1>".$row["type"]."</h1>";
}

echo($product);
Run Code Online (Sandbox Code Playgroud)

返回

<h1>MR001</h1>
<h1>MR001</h1>
<h1>MR002</h1>
<h1>MR003</h1>
<h1>MR003</h1>
<h1>MR004</h1>
<h1>MR004</h1>
<h1>MR004</h1>
Run Code Online (Sandbox Code Playgroud)

等,但我需要它只返回一次相同的值?

喜欢

<h1>MR001</h1>
<h1>MR002</h1>
<h1>MR003</h1>
<h1>MR004</h1>
Run Code Online (Sandbox Code Playgroud)

STUCK.COM帮助!!!

Dev*_*rke 6

更改要使用的查询DISTINCT,该查询仅返回唯一行:

mysql_query("SELECT DISTINCT type FROM certs ORDER by type")
Run Code Online (Sandbox Code Playgroud)

为便于使用,您的整个更正代码如下:

$query = mysql_query("SELECT DISTINCT type FROM certs ORDER by type") or die("Could not execute query");
while($row = mysql_fetch_array($query) )
{
    $product .= "<h1>".$row["type"]."</h1>";
}
echo($product);
Run Code Online (Sandbox Code Playgroud)


Cha*_*ndu 6

在SQL查询中使用DISTINCT ..试试这个:

$query = mysql_query("SELECT DISTINCT type FROM certs ORDER by type") or die("Could not execute query");

while($row = mysql_fetch_array($query) ) 
{     
   $product .= "<h1>".$row["type"]."</h1>"; 
}  
Run Code Online (Sandbox Code Playgroud)