I have a table that contains attribute values for products. The primary key is product_id, attr_id and the only other field is attr_val (text) which contains the value of the attribute.
I'm trying to create a dropBox of options and populate it with various attr_val 's given an attr_id. The table looks something like:
product_id attr_id attr_val (TEXT)
1 5 Silver
1 6 325
2 5 Tangerine Orange
3 5 Marroon
4 5 Silver
5 5 Silver
6 5 Silver
7 5 Marroon
7 6 200
I did not create an attribute_value table that would associate an integer with a attr_val_id such as "Silver=1" because I would also like to be able to execute queries like SELECT * Where attr_id =6 AND attr_val > 300; But maybe I should... ??😕
In this case, I'd like to create a box with the various attr_val for attr_id=5. This should produce selections for Silver, Marroon, and Tangerine Orange.
My query looks like:
$query = "SELECT attr_val FROM product_attr WHERE attr_id = '5' GROUP BY attr_val LIMIT 40";
$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
print('<select name="attr5">');
for($j=0;$j<$num_rows;$j++){
$row = mysql_fetch_array($result);
print('<option value="' . $row['attr_val'] . '">' . $row['attr_val'] . '</option>');
}
print('</select>');
(The row checks are just because I was running into problems with too many rows being inserted into the select boxes).
Basically the GROUP BY statement is causing the query to run very slowly (5 sec +). I tried adding a solo attr_id INDEX but it made no difference. If I want to add a attr_val index I will have to change it to a VARCHAR which will truncate many values that are over 255 char.
Thanks for any help!