我正在寻求帮助。我有3个表,我必须查询结果。在表格模型中,我有model_id
和model
,在表格颜色中,我有colour_id
和colour
,在表格可用性中,我有model_id
和colour_id
。我必须以表格格式回应结果。该表必须显示模型,颜色和可用性。在可用性部分中,如果模型以某种颜色可用,则必须显示/返回yes值,如果不显示no。这是ma当前代码:
$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>";
答案 0 :(得分: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
这可以为您提供所需的结果。只需根据自己的喜好格式化输出。