0

我如何知道选择了哪个 OR 语句?我希望 fetch 显示选择的 colour.col。

$parsed = array();
$result = mysql_query("SELECT *
FROM colour
LEFT JOIN shape ON colour.id = shape.id
WHERE shape.col1 = 'square' AND  colour.col1 = 'blue'
OR shape.col1 = 'square' AND  colour.col2 = 'pink'
OR shape.col1 = 'circle' AND  colour.col3 = 'red'
OR shape.col1 = 'triangle' AND  colour.col4 = 'yellow'
OR shape.col1 = 'rectangle' AND  colour.col5 = 'green'
");



while($row = mysql_fetch_array($result))
{ 
echo "<tr>";
echo "<td>" . $row[col1] . "</td>";

**which colour column was selected?**
I would like to see the column header here (colour.col1,colour.col2...) not the value    (pink,green...)

echo "</tr>";

}
4

4 回答 4

2

使用额外的列来指示所选颜色:

SELECT *, 
       case when shape.col1 = 'square' and colour.col1 = 'blue'
            then 'blue' 
            when shape.col1 = 'square' and colour.col2 = 'pink'
            then 'pink' 
            ...
       end as selected_color
FROM colour
where ...
于 2012-07-11T08:39:54.000 回答
0
$result = mysql_query("SELECT * FROM colour LEFT JOIN shape ON colour.id = shape.id
WHERE (shape.col1 = 'square' AND  colour.col1 = 'blue') 
OR (shape.col1 = 'square' AND  colour.col2 = 'pink') 
OR (shape.col1 = 'circle' AND  colour.col3 = 'red') 
OR (shape.col1 = 'triangle' AND  colour.col4 = 'yellow') 
OR (shape.col1 = 'rectangle' AND  colour.col5 = 'green')
");

我想应该是这样的......也许你可以在sql中尝试一下^^

于 2012-07-11T08:38:54.793 回答
0

我想它会帮助你!使用mysql_fetch_row

$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
    echo 'err: ' . mysql_error();
    exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // email
于 2012-07-11T08:42:17.057 回答
-2

像下面这样正确地格式化你的查询,你就会知道。

$result = mysql_query("SELECT *
FROM colour
LEFT JOIN shape ON colour.id = shape.id
WHERE (shape.col1 = 'square' AND  colour.col1 = 'blue')
OR (shape.col1 = 'square' AND  colour.col2 = 'pink')
OR (shape.col1 = 'circle' AND  colour.col3 = 'red')
OR (shape.col1 = 'triangle' AND  colour.col4 = 'yellow')
OR (shape.col1 = 'rectangle' AND  colour.col5 = 'green')
");
于 2012-07-11T08:35:36.913 回答