0

我正在寻求帮助。我有 3 个表,我必须从中查询结果。在表格模型中,我有model_idmodel,在表格颜色中,我有colour_idcolour在表格可用性中,我有model_idcolour_id。我必须以表格格式回显结果。该表必须显示型号、颜色和可用性。在可用性部分,如果模型有某种颜色,它必须显示/返回一个是的值,如果没有显示一个否。这是当前代码:

$sql = "SELECT model, colour FROM model, colour";  
   $query = "SELECT * FROM availability";    

   $result = mysql_query($sql);

   $res =mysql_query($query);

 echo "<table border='1'><trbgcolor='#cccccc'><td>Model</td><td>Colour</td><td>Availability</td></tr>";

    while($row = mysql_fetch_array($result))
    {
      $model=$row['model'];
      $colour=$row['colour'];

      echo "<tr><td>$model</td><td>$colour</td>";

      while($row = mysql_fetch_array($res))
      {
        $avail_id=$row['avail_id'];
        $model_id=$row['model_id'];
        $colour_id=$row['colour_id'];
      }


      if($res)
      {
        echo "<td>Yes</td>";
      }
      else
      {
        echo "<td>No</td></tr>";
      }
    } 

  echo "</table>";
4

1 回答 1

0
SELECT m.model, c.colour, IF(c.colour_id IN (SELECT a.colour_id FROM availability a WHERE a.model_id = m.model_id), "YES", "NO") FROM model m, colour c ORDER BY m.model, c.colour

这应该会给你你想要的结果。只需根据自己的喜好格式化输出。

于 2013-07-30T10:25:53.687 回答