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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:58:47+00:00 2026-06-02T23:58:47+00:00

I have this table that contains the ad id and the user id. Any

  • 0

I have this table that contains the ad id and the user id.
Any single user may have multiple ads.
I want to show only two ads per user (or one if he only has one). The output must be random.

CREATE TABLE IF NOT EXISTS `ads` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_user` int(12) NOT NULL,
  `title` varchar(255) DEFAULT NULL,
  `url` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

Table contains this:

1, 1, 'erfw', 'google.pt'
2, 2, 'yhtg', 'google.pt'
3, 2, 'gfddfg', 'google.pt'
4, 3, 'ehgfd', 'google.pt'
5, 2, '435yet', 'google.pt'
6, 1, 'ikkt', 'google.pt'
7, 1, 'wyrths', 'google.pt'

The output should be:

1, 1, 'erfw', 'google.pt'
2, 2, 'yhtg', 'google.pt'
3, 2, 'gfddfg', 'google.pt'
4, 3, 'ehgfd', 'google.pt'
6, 1, 'ikkt', 'google.pt'

The output must be different from time to time, to get all ads randomly.

  • 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-06-02T23:58:49+00:00Added an answer on June 2, 2026 at 11:58 pm

    In MySQL, doing any Greatest-n-per-group would make use of MySQL variables Preserve the last “thing”, and another for your “numbering” assignment (which you want as random in this case)

    select
          PreQuery.*,
       from
          ( select @lastUser := 0,
                   @lastAdSeq := 0 ) sqlvars,
          ( select
                  ads.id_user,
                  ads.id,
                  ads.title,
                  ads.url,
                  @lastAdSeq := if( @lastUser = ads.id, @lastAdSeq +1, 1 ) as Seq,
                  @lastUser := ads.id_user as carryover
               from
                  ads
               order by
                  ads.id_user,
                  rand() 
               having Seq <= 2 ) PreQuery
    

    The “HAVING” clause is critical to understand in that this clarifies that the “HAVING” clause is applied AFTER any “GROUP BY” or “ORDER BY” and is applied to AFTER the record is considered in the result set (and not optimized with any WHERE clause).

    That said, we query from ads, ordered by the user’s ID, but random second. This forces all the person’s “ads” as randomized. After that, the @ variables are applied and building up a sequence count per user. The having clause is then applied, and as soon as it gets to 3 or more, the results are thrown OUT of the final result set. And you are left with AT MOST 2 ads per user.

    needs to be done first to ensurshould query the data ordered

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

Sidebar

Related Questions

Greetings, fellow coders! I have this table that contains categories and subcategories (actually I
I want to have a form that contains a table of fields and some
I have a user table that has many columns, it looks roughly like this:
I have a table that contains some user data: user_id | guest_id | time_seen
I already have a table in phpmyadmin that contains users records. Each user has
I have a table that looks like this: table name: uno str_id | title
I have a table that someone update this table every day. I would like
I have a Pages table that stores all my view urls and this table
I have this table: id feed_id ... Let's say that I have 500 rows
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL

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.