我有三张桌子
product_color
===
color_id (primary)
color_initial
red
green
blue
school_art
===
id(primary)
art_loc
series_code
set_color
===
setcolors_id
school_art_id
color_id
我想做的是:
从 product_color 表中获取红色,绿色和蓝色,其中product_color.color_id = set_color.color_id
和set_color.school_art_id = school_art.id
这就是我所拥有的,但由于它不起作用,显然我错过了一些东西。任何帮助SOOO感激不尽。
$colors =
"SELECT * FROM
product_color
JOIN
set_colors
ON
product_color.color_id = set_colors.color_id
WHERE
set_colors.school_art_id = '{$school_art_id}'";
$colorresult = mysql_query($colors) or die(mysql_error());
$returncolors = mysql_fetch_array($colorresult);
答案 0 :(得分:2)
我假设你说school_art.id
,你也想要提取school_art
表。为此,您必须JOIN
所有三个表:
SELECT * FROM product_color
JOIN set_color ON(product_color.color_id = set_color.color_id)
JOIN school_art ON(school_art.id = set_color.school_art_id);
答案 1 :(得分:0)
$q = "SELECT * FROM product_color, set_color, school_art
WHERE product_color.color_id = set_color.color_id
AND school_art.id = set_color.school_art_id
AND set_colors.school_art_id = $school_art_id";
这应该适用于查询。
此外,您可能希望在循环中使用mysql_fetch_array()。
while ($row = mysql_fetch_array($colorresult)) {
allColors[] = $row;
}
现在allColors
数组的每个元素都包含一个结果行,因此您可以随意使用它。