如何使用ORDER BY RAND()与where条件

时间:2016-04-21 17:59:12

标签: php mysql cakephp-2.0

我想使用ORDER BY RAND()显示我的产品,其中where condition

这是我的查询

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

我知道这个正常的工作查询没有where条件

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

但我想使用where where

提前完成。

2 个答案:

答案 0 :(得分:0)

只需使用where

简单使用order by条件即可
$qry="SELECT *  FROM product_tbls
  WHERE category_tbls_id = '".$data["0"]["product_tbls"]["category_tbls_id"]."'
  ORDER BY RAND() 
  LIMIT 0,6;"

答案 1 :(得分:0)

如果这是你要问的:你应该在ORDER BY rand()之前使用“WHERE”。

示例:

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

您忘记连接查询的其余部分。

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

抱歉我的英文。