ethio
ethio

Reputation: 539

Using "JOIN", "WHERE", and "IN" (mysql+php)

SELECT attribute_value.value 
FROM attribute_value 
INNER JOIN attributes ON attributes.id=attribute_value.attribute_id
WHERE attributes.attribute IN (colour, size);

I'm not doing this right, but the error I'm getting is #1054 - Unknown column 'Colour' in 'where clause'

I'm trying to get the values of the attributes they are associated with. Right now the attributes are set out in the "IN" clause but I'd want to know how to use an associative array like this:

Array([attributes] => Array ( [0] => Colour [1] => Size )

Table attribute_value

______________________________
|  attribute_id  |  value    |
------------------------------
|  1             |  Red      |
|  1             |  Blue     |
|  2             |  Large    |

Table attributes

____________________
|  id  | attribute |
--------------------
|  1   |  Colour   |
|  2   |  Size     |

Just to clarify, multiple attributes will be posted and using only these attributes, get values from attribute_value where attribute id in attribute equals attribute_id in attribute_value

Upvotes: 1

Views: 81

Answers (2)

user2989408
user2989408

Reputation: 3137

Try this in your WHERE attributes.attribute IN ('colour', 'size').

Upvotes: 2

cardeol
cardeol

Reputation: 2238

You have a problem with IN. You should use strings between single quotes.

SELECT attribute_value.value 
FROM attribute_value 
INNER JOIN attributes ON attributes.id=attribute_value.attribute_id
WHERE attributes.attribute IN ('Colour', 'Size');

Upvotes: 7

Related Questions