How to add if statement in SQL ?

Posted by shin on Stack Overflow See other posts from Stack Overflow or by shin
Published on 2010-02-27T15:25:27Z Indexed on 2010/05/14 17:04 UTC
Read the original article Hit count: 346

Filed under:
|

I have a following MySQL with two parameters, $catname and $limit=1. And it is working fine.

SELECT P.*, C.Name AS CatName 
                   FROM omc_product AS P
                   LEFT JOIN omc_category AS C
                   ON C.id = P.category_id
                   WHERE C.Name = '$catname'
                   AND p.status = 'active'
                   ORDER BY RAND()
                   LIMIT 0, $limit

Now I want to add another parameter $order. $order can be either

ODER BY RAND()

or

ORDER BY product_order

in the table omc_product.

Could anyone tell me how to write this query please?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about beginner