Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:17:52+00:00 2026-05-30T20:17:52+00:00

I want get 10 rand result, which image !=” , group by uid and

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:17:53+00:00Added an answer on May 30, 2026 at 8:17 pm

    I’d do it with a join instead

    select *
      from article_table at
      join ( select uid
               from user_table
              where type = '1' ) ut
        on at.uid = ut.uid
     where image != ''
     group by at.uid
     order by rand()
     limit 10
    

    Or you may want to restrict the number of uids from your user_table to make it quicker to begin with:

    select at.*
      from article_table at
      join ( select uid
               from user_table
              where type = '1'
              order by rand()
              limit 10 ) ut
        on at.uid = ut.uid
     where image != ''
     group by at.uid
     order by rand()
     limit 10
    

    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 the order by in 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: $result = mysql_query(SELECT * FROM quote ORDER BY RAND() LIMIT
I want to get a files these attributes as integer values.
I want to select a random value from a array, but keep it unique
From an array $my_array = array('a','b','c','d','e'); I want to get two DIFFERENT random elements.
I want get the time used for a case so I can create an
I want get all of the Geom objects that are related to a certain
I want to get started doing some game development using Microsoft's XNA. Part of
I want to get the MD5 Hash of a string value in SQL Server
I want to get my databases under version control. I'll always want to have
I want to get an overview of files that are updated in TFS (that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.