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

  • SEARCH
  • Home
  • 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 6553515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:35:27+00:00 2026-05-25T12:35:27+00:00

I have limited programming experience, and almost none with PHP. However I’ve been given

  • 0

I have limited programming experience, and almost none with PHP. However I’ve been given a task that requires something more than my basic SQL skills cover… :/

I have a MySQL table (“Users”) that contains 334 distinct user IDs, and another table (“Revisions”) which contains all the revisions made to a wiki site, along with the ID of the user who committed each revision.

I want to generate a results file (tab delimited) that contains a random sample of 100 revisions for EACH of the users in the “Users” table, along with that user’s ID.

My SQL query looks like this:

select r.revision_id, r.revision_timestamp, r.user_id from revision as r, users as u where r.user_id = u.user_id order by rand() limit 100;

As currently written this gives me a random assortment of 100 revisions made by any of the users in the “Users” table. I want 100 random revisions for each user in the table.

I know this can be done with PHP, probably easily, but I don’t know the syntax. How can I accomplish this?

  • 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-25T12:35:28+00:00Added an answer on May 25, 2026 at 12:35 pm

    Does it have to be a single query? If not, it would be trivial to do an initial query for a list of users, then do a query for each user – the per user query would be identical to your current one, with the addition of where u.user_id = [user id from loop]. As far as syntax, any basic PHP mysql tutorial should give you enough to do the queries, then look at fopen/fwrite/fclose for generating the csv.

    Overall pesudocode would look something like:

    $file = file open "filename.csv"
    $queried_users = mysql query "select u.user_id from users u"
    
    foreach $queried_users as $user
        $entries = mysql query "select r.revision_id, r.revision_timestamp, r.user_id from revision as r, users as u where r.user_id = u.user_id and where u.user_id = " . $user['user_id'] . " order by rand() limit 100;"
        foreach $entries as $entry
            file write to $file $entry['user_id'].','.$entry['revision_id'].','.$entry['revision_timestamp'].'\n';
    
    file close $file
    

    Note that assembling queries by building strings like that is unsafe (better generally to use parameterized queries), but it sounds like this is an internal only thing, so you can probably get away with it here.

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

Sidebar

Related Questions

I have a very limited experience of database programming and my applications that access
First of all I have to admit that my programming skills are pretty limited
I am a web-developer working in PHP. I have some limited experience with using
I have a PROGRAMMING task (homework) to investigate brute force neural net methods. That
I'm developing a WinForms application in C#. I have limited experience in GUI programming,
I have limited experience with .net. My app throws an error this.dateTimeFormat is undefined
I'm trying to get started on combining my (slightly limited) PHP experience with my
I am good with ideas, but due to my limited programming experience I sometimes
I have always thought that functional programming can be done in Python. Thus, I
Im very new to C programing, but have limited experience with Python, Java, and

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.