MySQL Forums
Forum List  »  PHP

Random select
Posted by: Rahman Mousavian
Date: October 20, 2008 03:59AM

Hi all
I want to select 3 random category from database
then select 4 product from each selected category!

but old of my code is very slow
any one have any suggestion ?!

the old code is :



$link = mysql_connect('localhost', 'user', 'password');

$Rcats = mysql_query(" SELECT DISTINCT `main_categories_id` AS `categories_id`
FROM selected_products
WHERE 1
ORDER BY RAND(NOW()) LIMIT 3;",$link);




while ($Rcats_array = mysql_fetch_assoc($Rcats )) {
$cats [] = array ('categories_id' => $Rcats_array['categories_id']);
}


...
...
...
...
...

/// this will be run 3 times for each $cats :
mysql_query("INSERT INTO random_products (`products_id` , `products_image` , `products_price` , `products_name` , `categories_name` , `categories_id` , `categories_rank`)
SELECT DISTINCT products.products_id , products.products_image, products.products_price ,products_description.products_name , categories_description.categories_name, categories_description.categories_id , categories_description.categories_rank
FROM products , products_description , selected_products , categories_description
WHERE products.`products_id` = selected_products.`products_id`
AND products.`products_quantity` > 0
AND products.`products_status` = '1'
AND products.`products_id` = products_description.`products_id`
AND categories_description.categories_id = ".$cats [0]['categories_id']."
AND selected_products.`main_categories_id` = ".$cats [0]['categories_id']."
ORDER BY RAND(NOW())
LIMIT 4;",$link);



Edited 1 time(s). Last edit at 10/20/2008 04:11AM by Rahman Mousavian.

Options: ReplyQuote


Subject
Written By
Posted
Random select
October 20, 2008 03:59AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.