Need help finishing off crafting an SQL query for matching pairs of columns - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Need help finishing off crafting an SQL query for matching pairs of columns (/showthread.php?tid=51393) |
Need help finishing off crafting an SQL query for matching pairs of columns - El Forum - 05-02-2012 [eluser]The Questioner[/eluser] I've developed an ecommerce site in CI which allows the user to filter products by product attributes. Each product can have one or more product attribute. I have a model which builds the SQL directly to retrieve the appropriate rows from the MySQL table. This table is used to store attributes for each product. Its structure is like this: Code: CREATE TABLE IF NOT EXISTS `product_attributes` ( The id_attribute column contains an ID number, and the allowed_attribute_items column contains a string of comma seperated numbers. Imagine if the table contained the following data: Code: row 1: I want to retrieve distinct id_product row values where id_attribute is 6 and the associated allowed_attribute_items contains the number 194, AND where id_attribute is 8 and the associated allowed_attribute_items contains the number 206. In this case rows 2 and 3 should be retrieved. The SQL needs to apply multiple combinations of the row pair id_attribute and allowed_attribute_items. The closest syntax I can get is: Code: SELECT DISTINCT id_product Using this as another example, I want to get hold of all the distinct id_product values where the id_attribute is <value1> and the associated allowed_attribute_items contains the value <value2> AND also where id_attribute is <value3> and the associated allowed_attribute_items contains the value <value4>. However the above SQL syntax doesn't work. I don't want to use an OR because the criteria need to match for each pair of columns. I just can't get this to work. |