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 672735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:31:02+00:00 2026-05-14T00:31:02+00:00

Hello i have a question on picking random entries from a database. I have

  • 0

Hello i have a question on picking random entries from a database. I have 4 tables, products, bids and autobids, and users.

Products
-------  
id 20,21,22,23,24(prime_key)
price...........
etc...........

users  
-------
id(prim_key)  
name user1,user2,user3  
etc  

bids  
-------
product_id  
user_id  
created  

autobids  
--------
user_id   
product_id 

Now a multiple users can have an autobid on an product. So for the next bidder I want to select a random user from the autobid table

example of the query in language:

for each product in the autobid table I want a random user, which is not the last bidder.

On product 20 has user1,user2,user3 an autobidding.
On product 21 has user1,user2,user3 an autobidding

Then I want a resultset that looks for example like this

20 – user2
21 – user3

Just a random user. I tried miximg the GOUP BY (product_id) and making it RAND(), but I just can’t get the right values from it. Now I am getting a random user, but all the values that go with it don’t match.

Can someone please help me construct this query, I am using php and mysql

  • 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-14T00:31:03+00:00Added an answer on May 14, 2026 at 12:31 am

    The first part of the solution is concerned with identifying the latest bid for each product: these eventually wind up in temporary table “latest_bid”.

    Then, we assign randon rank values to each autobid for each product – excluding the latest bid for each product. We then choose the highest rank value for each product, and then output the user_id and product_id of the autobids with those highest rank values.

    create temporary table lastbids (product_id int not null, 
                                     created datetime not null, 
                                     primary key( product_id, created ) );
    
    insert into lastbids 
    select product_id, max(created)
    from bids
    group by product_id;
    
    create temporary table latest_bid ( user_id int not null, 
                                        product_id int not null, 
                                        primary key( user_id, product_id) );
    
    insert into latest_bid
    select product_id, user_id 
    from bids b
    join lastbids lb on lb.product_id = b.product_id and lb.created = b.created;
    
    create temporary table rank ( user_id int not null, 
                                  product_id int not null, 
                                  rank float not null, 
                                  primary key( product_id, rank ));
    
    # "ignore" duplicates - it should not matter
    # left join on latest_bid to exclude latest_bid for each product
    
    insert ignore into rank 
    select user_id, product_id, rand() 
    from autobids a
    left join latest_bid lb on a.user_id = lb.user_id and a.product_id = lb.product_id 
    where lb.user_id is null;
    
    create temporary table choice 
    as select product_id,max(rank) choice 
       from rank group by product_id;
    
    select user_id, res.product_id from rank res
    join choice on res.product_id = choice.product_id and res.rank = choice.choice;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello guys sorry if the question looks stupid to you. i have 3 tables
Hello wonderful stackoverflow users. I have a question about url loading. In many browsers
Hello Stackoverflow users, I have a question regarding an JQ UI widget im trying
hello i have this question i was trying to find a way to call
Hello is have a question for a school assignment i need to : Read
hello I have a small question in objective-C memory management. I know that if
Hello this is may first question and I have found so far the following
I have the following regexp: (?P<question>.+(?<!\[\[)) It is designed to match hello world! in
i have this: <div id=parent style=overflow:auto> ... <div id=test>Hello</div> ... </div> My question is:
I have a string, Hello there I have a question I want to delete

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.