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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T15:13:05+00:00 2026-06-01T15:13:05+00:00

I have a MySQL table like id | text | category | active I

  • 0

I have a MySQL table like

id    |  text   |  category   |   active

I select a random line with

SELECT id, text 
FROM table
WHERE category = [category id] AND active = 1 
ORDER BY RAND()
LIMIT 1

Some times this would return no results (e.g. if there is no active row in a specific category). What I need to do in that case is to return a “default” row.

My question is: what is the most efficient way to do this? Should I just create an identical table but just with the default rows, which I would query if the above query gives no results? Or should I add the default rows in the same table? And how would you query it?

Any suggestion is welcome!

EDIT
A few updates to the question:

  1. I am excluding the possibility of generating the default text in PHP, as I want it to be customizable, without having to go and change the code.

  2. There will be a default row per category

  • 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-01T15:13:07+00:00Added an answer on June 1, 2026 at 3:13 pm

    What is the default value you want? The 1st id in the table?

    SELECT id, text
    FROM (  SELECT id, categori, active, text, 0 AS prio
            FROM table WHERE category = [category id] AND active = 1 
            UNION ALL
            SELECT id, categori, active, text, 1
            FROM table
            ORDER BY id ASC
            LIMIT 1) AS h
    ORDER BY prio, RAND()
    LIMIT 1
    

    If you wish for a certain default value, add a ENUM column called default('TRUE', 'FALSE'):

    SELECT id, text
    FROM (  SELECT id, categori, active, text, 0 AS prio
            FROM table WHERE category = [category id] AND active = 1 AND default = 'FALSE'
            UNION ALL
            SELECT id, categori, active, text, 1
            FROM table
            WHERE category = [category id] AND default = 'TRUE'
            ORDER BY id ASC
            LIMIT 1) AS h
    ORDER BY prio, RAND()
    LIMIT 1
    

    Easier one:

    SELECT id, text
    FROM table
    WHERE category = [category id] AND (active = 1 OR default = 'TRUE')
    ORDER BY (default = 'FALSE') DESC, RAND()
    LIMIT 1
    

    Explanation:
    Basicly this query fetches all the rows you count on to get, and combine it with a default row. They got a prio value each, which you order them in before doing RAND(). The thing remaining is how you wish to store your default rows.

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

Sidebar

Related Questions

I have a MySQL query like this SELECT id,seq,text FROM table ORDER BY seq
I have a MySQL table that looks like this: id (int primary) name (text)
I have a MySql MyISAM table with a full text index, like this: CREATE
I have a MySQL table like this: Payments +----+---------------------+---------+ | id | date (sorted
I have a MySQL table looking like this: > describe books; +---------------+--------------+------+-----+---------+----------------+ | Field
I have a MySQL table structured somewhat like this: type name value ===================== 1
I have a MySQL table that looks like this: `id` int(10) unsigned NOT NULL
I have this mysql table called comments which looks like this: commentID parentID type
I have a mySQL table that has a column like this: ID ----- 0352
I have created a MySQL table and would like to save the contents of

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.