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

The Archive Base Latest Questions

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

Using MySQL and PHP. I’m trying to select 4 random ads from a table.

  • 0

Using MySQL and PHP. I’m trying to select 4 random ads from a table. 1 for adspot 1, 1 for adspot 2, 1 for adspot 3, 1 for adspot 4. Here is what my table looks like:

ad_id | ad_spot | ad_html

1 | 3 | <ad html>
2 | 1 | <ad html>
3 | 4 | <ad html>
4 | 2 | <ad html>

There are about 1200 ads in the table with multiple records for each ad spot and the records can change at anytime. Here is the query I use to select 1 random ad for a specific adspot:

SELECT ad_html FROM ads WHERE ad_spot = '2' ORDER BY RAND() LIMIT 1

I timed a script that selected all 1200 records and put them in a PHP array, then randomly chose 1 for each adspot. That took longer on average than running 4 MySQL queries, with different where clauses, to select the ads with RAND().

What is the most efficient way to combine the following queries into a single query?

SELECT ad_html FROM ads WHERE ad_spot = '1' ORDER BY RAND() LIMIT 1
SELECT ad_html FROM ads WHERE ad_spot = '2' ORDER BY RAND() LIMIT 1
SELECT ad_html FROM ads WHERE ad_spot = '3' ORDER BY RAND() LIMIT 1
SELECT ad_html FROM ads WHERE ad_spot = '4' ORDER BY RAND() LIMIT 1
  • 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:59:08+00:00Added an answer on May 30, 2026 at 8:59 pm
    SELECT
       MAX(IF(row_num = 1, ad_html, NULL)) AS 'ad_space_1',
       MAX(IF(row_num = 2, ad_html, NULL)) AS 'ad_space_2',
       MAX(IF(row_num = 3, ad_html, NULL)) AS 'ad_space_3',
       MAX(IF(row_num = 4, ad_html, NULL)) AS 'ad_space_4'
    FROM
    (
       SELECT
          @row_num := @row_num + 1 AS 'row_num',
          ad_html
       FROM    
          (SELECT  
             @cnt := COUNT(*) + 1,
             @lim := 4,
             @row_num := 0
          FROM
             ads
          ) vars
       STRAIGHT_JOIN
          (
          SELECT
             r.*,
             @lim := @lim - 1
          FROM    
             ads r
          WHERE   
             (@cnt := @cnt - 1)
             AND RAND(203121231) < @lim / @cnt
          ) i
    ) j
    

    Give it a random seed each query from your php (timestamp or such). Alternatively you could omit the outer query and the inner query will return 4 rows which you can iterate through in your php code.

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

Sidebar

Related Questions

I am trying to create a table in MySql using php. My code looks
I am using mysql/php. my table looks like id (int autoincrement) voucher(varchar 25) I
I'm using MySQL with PHP. This is like my table: (I'm using 3 values,
I currently am using mysql and php to display 9 random results from my
I'm trying to connect to remote MySQL server with SSL from PHP using mysql_connect:
I am using MYSQL and PHP. I have a table called item. Two of
How to fetch the first two rows from Mysql DB using Mysql PHP function?
using mysql and php, I have two tables, I'm french so the table names
Using MySQL and PHP; I'm trying to build an index that contains the averages
I'm trying to develop a system using MySQL and PHP. I'm using mysqli* function

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.