Vky Vky - 7 months ago 6
SQL Question

how to use ORDER BY RAND() with where condition

i want to display my products using ORDER BY RAND() with where condition

This is my query

$qry="select * from product_tbls where category_tbls_id=".$data["0"]["product_tbls"]["category_tbls_id"];


i know this normal working query without where condition

$qry="select * from product_tbls ORDER BY RAND() LIMIT 0,6;";


but i want to use where condition

Thanx in advance.

Answer

If this is what you're asking: You should use "WHERE" before ORDER BY rand().

Example:

$qry="select * from product_tbls WHERE a = 'b' ORDER BY RAND() LIMIT 0,6;";

You forgot to concatenate the rest of the query.

$qry="select * 
      from product_tbls 
      where category_tbls_id=".$data["0"]["product_tbls"]["category_tbls_id"]." 
      ORDER BY RAND() LIMIT 0,6";

Sorry for my english.