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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.