3

基本问题!

我有 2 张桌子

生产

   +-----+--------------+  
   | id  |  fruit_name  |
   +--------------------+
   | 1   |   Apple      |
   | 2   |   Banana     |
   | 3   |   Carrot     |
   +-----+--------------+

品种

   +-----+---------------+----------------+
   | id  |  fk_fruit_id  |  variety_name  |
   +-----+---------------+----------------+
   | 1   |   1           |    Cox         |
   | 2   |   1           |    Braeburn    |
   | 3   |   2           |    Chester     |
   | 4   |   3           |    Kotaka      |
   | 5   |   3           |    Imperial    |
   | 6   |   3           |    Oneal       |
   +-----+---------------+----------------+

我想输出每个水果的品种列表,例如

APPLE - Cox, Braeburn

BANANA - Chester

CARROT - Kotaka, Imperial, Oneal

我目前的代码是

$query   = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id"; 

$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
while ($row     = mysql_fetch_array($result, MYSQL_ASSOC)) { 

$produce_fruit_code   = $row['fruit_code']; 
$variety_name   = $row['variety_name']; 

echo $produce_fruit_code.' - '.$variety_name.'<br/>';

}

输出:

Apple - Cox
Apple - Braeburn
Banana - Chester
Carrot - Kotaka
Carrot - Imperial
Carrot - Oneal

不是一百万英里之外,但仍然不在那里。非常感谢任何帮助,谢谢!

4

6 回答 6

1

你可能会得到一个粗壮的 sql 语句来为你做这件事,但我会选择使用数组来处理数据。

例如(未经测试,请原谅格式):

$query   = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id";     
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 

$res=array();

while ($row     = mysql_fetch_array($result, MYSQL_ASSOC)) { 

    $produce_fruit_code   = $row['fruit_code']; 
    $variety_name   = $row['variety_name']; 

    if(isset($res[$produce_fruit_code])){
        $res[$produce_fruit_code]+=','.$variety_name;
    }else{
        $res[$produce_fruit_code]=$variety_name;
    }

}        
print_r($res);
于 2012-04-06T23:18:18.297 回答
1

如果你使用 MySQL,你可以在分组上使用 group_concat 扩展。类似于以下内容:

SELECT 
   f.fruitname as fruit, 
   GROUP_CONCAT(distinct v.varietyname separator ',') as variety  
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;

或类似的。抱歉,我的 sql 现在有点生锈了。有关更多信息,请查看本文 http://www.mysqlperformanceblog.com/2006/09/04/group_concat-useful-group-by-extension/ ,当然还有:http: //dev.mysql.com/doc/refman /5.0/en/group-by-functions.html

如果您不使用 MySQL 并且您的数据库不支持 group_concat,请考虑缓冲这些结果。在大型数据库和同时有许多用户的情况下,当每次都必须下载所有数据并将其存储在本地时,您的应用程序可能会大大减慢速度。

于 2012-04-06T23:43:58.897 回答
0

这不会让你一路走好,但它会让你得到你想要的大部分。有一些边缘情况是有问题的。

$query   = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id"; 
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 

$produce_fruit_code = "";
while ($row     = mysql_fetch_array($result, MYSQL_ASSOC)) { 
  if ($produce_fruit_code != $row['fruit_code'])
  {
    $produce_fruit_code = $row['fruit_code'];
    echo "<br/>".$produce_fruit_code." - ". $row['variety_name'];
  } else {
    echo ", ".$row['variety_name']; 
  }
}
于 2012-04-06T23:11:35.203 回答
0
$query   = "SELECT * FROM produce, varieties WHERE produce.id = varieties.fk_fruit_id"; 

echo "<dl>";

$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
while ($row     = mysql_fetch_array($result, MYSQL_ASSOC)) { 

    if($row['fruit_name'] != $current_fruit_name) {
        $current_fruit_name = $row['fruit_name'];
        echo "<dt>$current_fruit_name</dt>"; 
     }
echo "<dd>" . $row['variety_name'] . "</dd>";

}

    回声“”;

如果您想要一些 CSS 来使定义列表看起来像Name - X,Y,Z问题中的样子,请告诉我。

于 2012-04-06T23:27:30.930 回答
0

这个可以直接查询

SELECT 
   f.fruitname as fruit, 
   GROUP_CONCAT(distinct v.varietyname separator ',') as variety  
FROM fruit f JOIN varieties v ON produce.id = varieties.fk_fruit_id;
GROUP BY produce.id
于 2012-04-07T00:07:28.813 回答
-3

将所有内容排序到while循环中的数组中。这应该有效:

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { 

   $produce_fruit_code   = $row['fruit_code']; 
   $variety_name   = $row['variety_name']; 

   if ($produce_fruit_code == "Apple") {
   $apple_array[] = $variety_name;
   }
   if ($produce_fruit_code == "Banana") {
   $banana_array[] = $variety_name;
   }
   if ($produce_fruit_code == "Carrot") {
   $carrot_array[] = $variety_name;
   }

}

echo "Apples:" . implode(", ", $apple_array) . "<br/>";
echo "Bananas:" . implode(", ", $bananas_array) . "<br/>";
echo "Carrots:" . implode(", ", $carrots_array) . "<br/>";
于 2012-04-06T23:17:35.617 回答