Hi Dougal & others,
Well it's quite a big table. But I'll list the fields that matter:
autoID int(11) PRI NULL auto_increment
product_naam tinytext
product_categorie tinytext
product_status int(11) 0
product_last_mut datetime 0000-00-00 00:00:00
What i want to do is possible using multiple selects but I thought maybe this is too much work for the db and possible there is a better way of doing it.
Like I said earlier I have a php function that returns 6 unique random numbers (categories) out of the 10 possible (categories). Mysql has to go round and collect the productnames of the products that fit the categories.
Let's say the random function returns: 1, 3, 5, 6, 7, 10. So an easy way of doing this would be:
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 1; %' ORDER BY product_last_mut DESC LIMIT 0,1
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 3; %' ORDER BY product_last_mut DESC LIMIT 0,1
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 5; %' ORDER BY product_last_mut DESC LIMIT 0,1
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 6; %' ORDER BY product_last_mut DESC LIMIT 0,1
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 7; %' ORDER BY product_last_mut DESC LIMIT 0,1
SELECT product_name FROM products WHERE product_status < '3' AND product_categorie LIKE '% 10; %' ORDER BY product_last_mut DESC LIMIT 0,1
I use LIKE '% 1; %' because a product can fall into multiple categories, the product_categorie is tab and semicolon spaced, so it can be like this: " 1; 2; 4; 6; "
Problem with the above SELECT procedure is that the SELECTS in theory could return one and the same product for each SELECT if the product happens to fall into product_categorie 1, 3, 5, 6, 7 and 10.
A solution to this would be to have 6 queries. The first one returning the product_name. Then in the second SELECT having the statement WHERE product_name != '$product_name_of_first_query'. But doing 6 queries is rediculous...
Any ideas?
Cheers