我如何知道选择了哪个OR语句? 我希望获取显示被选中的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>";
}
答案 0 :(得分: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 ...
答案 1 :(得分: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 ^^
中尝试答案 2 :(得分: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
答案 3 :(得分:-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')
");