0

どの OR ステートメントが選択されたかを知るにはどうすればよいですか? 選択した color.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 に答える