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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:56:45+00:00 2026-05-26T10:56:45+00:00

How to express in a MySQL query: get all new elements but at least

  • 0

How to express in a MySQL query: “get all new elements but at least N elements”.

I’ll try to explain better. I want to know how to make a query if I have a product list [product, price, date, new] and I want to show the user all new elements, but if there are less than N elements (e.g. 5) show also the last items. If could think in a query like:

SELECT * FROM `products` WHERE new='1' ORDER BY date DESC 

but this just returns all new elements and this:

SELECT * FROM `products` WHERE new='0' ORDER BY date DESC LIMIT 5

just returns the minimum elements required.

Some examples:

  • If the table contains 4 new items, the query should return 4 new items and the latest non-new item
  • If the table contains 7 new items, the query should return just the 7 new items
  • If the table contains 1 new item, the query should return the new item and 4 non-new items.
  • 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-26T10:56:46+00:00Added an answer on May 26, 2026 at 10:56 am

    Use UNION to combine two result sets.

    (
        SELECT *
        FROM `products`
        WHERE new = '1'
    )
    UNION
    (
        SELECT *
        FROM `products`
        ORDER BY new DESC, date DESC
        LIMIT 5
    ) T1
    ORDER BY new DESC, date DESC
    
    • The first part of the UNION returns all the new items.
    • The second part of the UNION returns the first five new items, or if there are fewer than 5 items, all the new items and then first 5-n old items.
    • The UNION removes any duplicates in the result set.

    Note that this assumes that there is a unique key defined on any column or combination of columns in your table (for example, if your table has a primary key this should work).

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

Sidebar

Related Questions

I found some manual here : http://support.microsoft.com/kb/316898/ru but can't make a step with :
Can we create a sql express-like database using MySql. meaning: the database files can
I have a problem to express the next statement on mySql to return true
I´ve got a special problem, that first seems very easy but I don´t know
I need to connect to a MySQL database via C# express 2008. I think
I am creating a database application using Visual C# Express and MySQL. The issue
What does SQL Server Express offer that MySQL do not? PS: I'm comapring the
Just wondering what's a better way to write this query. Cheers. SELECT r.user_id AS
Can I get the whole query which I used for creating a table, like
I got this simple script ulizting express framework and node-mysql. Basically, the code listens

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.