I want get 10 rand result, which image !='', group by uid and these uid are the same from select uid from user_table where type='1'. but my query only return 2 result. where is the problem?
select * from article_table where image!=''
order by rand()
group by uid
in (select uid from user_table where type='1')
limit 10
I’d do it with a
joininsteadOr you may want to restrict the number of
uids from youruser_tableto make it quicker to begin with:I’m assuming here that there are many articles to each user. Although it looks more horrible, the
order by rand()in the inner select is over a smaller dataset, which’ll speed things up and theorder byin the outer select only has to deal with a smaller number of rows.Be careful, ordering by a random value can incur a significant performance hit as you have to go through the entire table that matches your where clause. There are alternatives.