# Mysqli random row in GROUP BY clause

**URL:** <https://www.phphelp.com/t/mysqli-random-row-in-group-by-clause/33118>\
**Category:** MySQL database\
**Created:** [February 11, 2021, 1:32pm UTC](https://www.phphelp.com/t/mysqli-random-row-in-group-by-clause/33118 "2021-02-11T13:32:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![COLT](https://www.phphelp.com/letter_avatar_proxy/v4/letter/c/c67d28/32.png) [@COLT](https://www.phphelp.com/u/COLT)\
**Post date:** [February 11, 2021, 1:32pm UTC](https://www.phphelp.com/t/mysqli-random-row-in-group-by-clause/33118/1 "2021-02-11T13:32:46Z")

</div>

I have the following query mysqli query by database:

> SELECT a.num, a.general\_num, a.url\_alias, a.title  
> FROM `m_catalog_products` AS a  
> LEFT JOIN `m_catalog_products_prices` AS b ON a.num=b.num  
> LEFT JOIN `m_catalog_products_colors` AS c ON a.num=c.num  
> WHERE `status` = ‘1’  
> AND `access` = ‘guest’  
> AND a.categ = 61  
> GROUP BY a.general\_num  
> ORDER BY IF(ind\_price\_4 \> 0,ind\_price\_4,9999999) ASC  
> LIMIT 0,24;

The problem is that I have multiple variations of products that have the same “general\_num” and I have to do group by that column because it’s all the same product and the only difference is in color and size.

Order by must be by price as it is, but the problem is that the query always retrieves the first product in “group by”, and I need a random product.

Ex. the products are 100100, 100101, 100102 … 100115 general num is here 1001, and this query always returns 100101, but I need some random (eg 100104).

EDIT: basically i have products with codes 100101, 100102, 100103, 1002104, 100105, 100106, 100107, 100108, 100109 … and they all have a column “general\_num” identical (1001 - first four digits) and when I do “group by” query always I return the product 100101 (first one), but I would like to return one of these (100101,100102 … 100109) by random

---

<div class="post-metadata">

**Author:** ![MacDknife](https://www.phphelp.com/user_avatar/www.phphelp.com/macdknife/32/2161_2.png) [@MacDknife](https://www.phphelp.com/u/MacDknife)\
**Post date:** [February 15, 2021, 9:47am UTC](https://www.phphelp.com/t/mysqli-random-row-in-group-by-clause/33118/2 "2021-02-15T09:47:37Z")

</div>

[https://stackoverflow.com/questions/249301/simple-random-samples-from-a-sql-database](https://stackoverflow.com/questions/249301/simple-random-samples-from-a-sql-database)
