Forum Moderators: coopster

Message Too Old, No Replies

Select by group and randomly

         

defanjos

1:43 am on Jan 11, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can someone point me in the right direction.

Let's say I have a table with 2 fields - listing_id, list_type.
I want to list all listings grouped by list_type and randomly within their list type
Would like to do it with only one select statement (PDO)
One foreach would be better, but could do it with 3 if needed.


Something like:

list_type 1
=============
7
3
2

list_type 2
=============
5
1
4

list_type 3
=============
8
6
9

The numbers represent listing_id

Thanks in advance.

defanjos

3:05 am on Jan 11, 2020 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I figured it out.
Did a select for all the list types (3)
Did a random select for all listings

Iterated list types and within the iteration, iterated the listings. Then checked if the list type in the outer iteration (list types) matched the inner iteration (listings), if so, displayed results.

I know, probably a confusing explanation.
It seems to work fine.