Jump to content

select query with random order, limit #, but with specific 1st item if found


dsdsdsdsd

Recommended Posts

I have my_table:

 

col_1 | col_2

-------------

aaa | mmm

bbb | nnn

ccc | ooo

ddd | ppp

eee | qqq

fff | rrr

 

 

I have php:

$max_return = 4 ;

$priority_val = 'ddd'

 

I want rows:

- if ddd is found it should be first else ignore it

- all the rest should be randomly picked/ordered

- no more than max rows returned

 

 

 

any thoughts?

 

thanks,

Shannon

you can do a WHERE col_1 ddd

You have do the rand yourself because I dont think you can do it in a query.

you can check for ddd before the query final query

you can LIMIT the search to 4.

 

find it all here

http://www.w3schools.com/sql/default.asp

this works, though messy:

 

 

( select *  from my_table  where col_A  = 'something'  and approved = 'true'  )

union

( select *  from my_table where col_A != 'something'  and approved = 'true'  ORDER BY RAND() limit 0,5  )

 

 

the only problem is that the max number, 5, should be conditioned upon whether first select succeeded or not ... but not a big deal.

if first select succeeded, then max = max-1 // max = 4

else max = max // max = 5

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.