我有 3 张桌子。第一个:“属性”第二个:“属性值”第三个:“产品”
我首先有这个查询:
SELECT a.*, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE, p.KATEGORIA
FROM atributy a JOIN produkty p ON p.ATTRIBUTE_CODE
LIKE CONCAT('%', a.code, '%')
AND
KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value
我的第二个查询是这样的:
SELECT * FROM atributy_value
INNER JOIN produkty
ON produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND AttributeCode = '$atribut_kod'
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value
请帮助我,让这 2 个查询的 1 个更好。原因:加载我的网上商店的时间过长。
编辑:
$query = mysql_query("
SELECT a.*, p.ATTRIBUTE_CODE, p.ATTRIBUTE_VALUE, p.KATEGORIA
FROM atributy a JOIN produkty p ON p.ATTRIBUTE_CODE LIKE CONCAT('%', a.code, '%')
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY a.value ");
while($result = mysql_fetch_object($query)){
$atribut_kod = $result->code;
$atribut_value = $result->value;
$nazov_produktu = $result->NAZOV;
$value1 = $result->ATTRIBUTE_VALUE;
$value1 = explode(" ", $value1);
$value1_count = count($value1);
echo "<div class=\"parametre_panel\">
<h3>".$atribut_value."</h3>
";
$url_kody .= "$atribut_kod,";
$hodnoty_qry = mysql_query("
SELECT * FROM atributy_value
INNER JOIN produkty ON produkty.ATTRIBUTE_VALUE LIKE CONCAT('%', atributy_value.ValueCode, '%')
AND AttributeCode = '$atribut_kod'
AND KATEGORIA IN ('$kategoria_sql')
GROUP BY atributy_value.Value ");
while($hodnoty_res = mysql_fetch_object($hodnoty_qry)){
$cislo_hodnoty = $hodnoty_res->ValueCode;
echo "<input type=\"checkbox\" class=\"ZobrazParametrickeVyhladavanie\" name=\"value[]\" id=\"$cislo_hodnoty\" value=\"".$atribut_kod."-".$cislo_hodnoty."\"><label for=\"$cislo_hodnoty\">".$hodnoty_res->Value."</label>
";
$url_hodnoty .= "$cislo_hodnoty,";
} //second query while()
echo "</div>";
} //first query while()
编辑2:
我的表结构
produkty: http://i.imgur.com/J4Kz2CE.png
atributy_value: http://i.imgur.com/nX1uRph.png
atributy: http://i.imgur.com/mlCa3It.png
索引:
atributy: http://i.imgur.com/ppMEEOe.png
atributy_value: http://i.imgur.com/RHAeSiu.png
produkty: http://i.imgur.com/IUrgy9l.png