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


how to use ORDER BY RAND() with where condition

>我想使用 ORDER BY RAND() 显示我的产品

,条件为

这是我的查询

$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;";

但我想使用条件

提前感谢。

如果这是你要问的:你应该在 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";

对不起我的英语。

只需简单地使用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;"