If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > can I do this with a single query and no PHP?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-06-10, 12:23
uhm uhm is offline
Registered User
 
Join Date: Mar 2010
Posts: 8
can I do this with a single query and no PHP?

Hello

Let's say I retrieve the most viewed productIDs with this query:

Code:
SELECT productid, COUNT(productid) AS views FROM products WHERE products.merchantcategory = 'categoryname') ORDER BY views DESC LIMIT 3
then I'll check with php the number of returned rows, if this number is 0 I'll run the query again but replacing this category with its parent:

Code:
SELECT productid, COUNT(productid) AS views FROM products WHERE products.merchantcategory = 'parentcategoryname' ORDER BY views DESC LIMIT 3
I suppose this can be done in a smarter way within the query, but I don't understand why

I tried searching for IF condition samples but honestly I can't get them to work ( MySQL :: MySQL 5.0 Reference Manual :: 11.4 Control Flow Functions )

basically I'd like to run a query like this:
Code:
IF (query1 returns 0 rows) { execute euery2 and return its results} else { return query1 results }
any suggestions?

Last edited by uhm; 09-06-10 at 13:27.
Reply With Quote
  #2 (permalink)  
Old 09-06-10, 17:17
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 620
Hi,

you will need to look at either stored procedures or functions in MySQL. This will more or less help you out. If the results of the productid has any results for the cat input into the stored procedure then we accept this. If on the other hand there are no results we use the parent category passed into the stored procedure as the second parameter.

Code:
DELIMITER //
CREATE PROCEDURE query (cat IN VARCHAR, parentcat IN VARCHAR)
BEGIN
  DECLARE count int;

  SELECT COUNT(productid) INTO count
  FROM products
  WHERE merchantcategory = cat;

  IF count > 0 THEN
     SELECT productid, COUNT(productid) AS views
     FROM products
     WHERE merchantcategory = cat
     ORDER BY views DESC LIMIT 3;
  ELSE
     SELECT productid, COUNT(productid) AS views
     FROM products
     WHERE merchantcategory = parentcat
     ORDER BY views DESC LIMIT 3;
  END IF;

END//
DELIMETER ;
To call this from within your PHP code you need to execute the following (not sure of the exact syntax):

Code:
mysql_query(db, "call query('categoryname','parentcategoryname')");
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #3 (permalink)  
Old 10-08-10, 16:48
uhm uhm is offline
Registered User
 
Join Date: Mar 2010
Posts: 8
sorry for my late response, thank you for your help Ronan
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On