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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:32:24+00:00 2026-05-24T08:32:24+00:00

Hi I am trying to get a mysql query to work properly but to

  • 0

Hi I am trying to get a mysql query to work properly but to be honest Im not sure where to start, I think count function would work but not sure.

What I am trying to do is do a select but if there are less than 5 rows counted than union with another query/where clause until 5 is reached.

So basically go at least 5 rows and keep joining rows until 5 or more is reached…

    $myquery_mysql = mysql_query("(SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND city = '".mysql_real_escape_string($GLOBALS['user_city'])."')
    UNION ALL
    (SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND province = '".mysql_real_escape_string($GLOBALS['user_province'])."' )
    UNION ALL
    (SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' )" or die(mysql_error());

Would I do something like…

            $myquery_mysql = mysql_query("(SELECT title, city, ad_image, rent, uniqid count(if title > 5) THEN FROM rentals WHERE front_page_ad = '1' AND paid = '1' )"
    UNION ALL
    (SELECT title, city, ad_image, rent, uniqid FROM rentals WHERE front_page_ad = '1' AND paid = '1' AND province = '".mysql_real_escape_string($GLOBALS['user_province'])."' ) 
    ELSE FROM rentals WHERE front_page_ad = '1' AND paid = '1' )" END ;

or die(mysql_error());

I know this isn’t right, but I am just trying to figure it out.

  • 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-24T08:32:25+00:00Added an answer on May 24, 2026 at 8:32 am

    You could add a LIMIT 5 at the end, so it will do the entire query with all the unions and then select the top 5 results, as long as you’re not giving it an ORDER BY it should list them in the order you do your unions.

    So if the first query has 5 results it’ll just take those, but if not, it’ll take results from the next 2 unions. As long as your unions aren’t very time consuming this seems like a good course of action.

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

Sidebar

Related Questions

I have been trying to get this query to work but I am stumbed
I'm trying to get this query to work in CodeIgniter but it's spitting out
Im trying to get this query to work but i get this error: Unknown
I've been trying to get this complex MYSQL query to work exactly right over
I'm trying to get the $url value to display from the MySQL database but
I have tried several things but I can't get the MySQL query in my
I'm trying to get this to work, but I am having issues. What am
I have a MySQL query that is trying to get all the Pages that
I'm trying to get this function to work where it returns the file location
I'm trying to get the rows of my mysql table into Arrays, but I

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.