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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:47:04+00:00 2026-06-05T14:47:04+00:00

Let’s say I have this table. Let’s say it looks something like this: table

  • 0

Let’s say I have this table. Let’s say it looks something like this:

table values (

 - id (int, primary key, auto increment) 
 - object_id (foreign key on a
   different table. Used to search for specific object values) 
 - year
   (varchar, because in someone's infinite wisdom using "1981-1982" in
   addition to 1981 and 1982 values is a good idea) 
 - high_price 
 - low_price

)

Now, this would be an easy thing to do if the data was simply added at the end with updated prices. However, the data is not nice looking. For instance, let’s look at a three-year range:

(id, object_id, year, high_price, low_price)

 - 259, 60, 1976, 34000, 29000
 - 260, 60, 1977, 35000, 31000
 - 261, 60, 1978, 36000, 35000
 - 1103, 60, 1976, 29000, 25000
 - 1104, 60, 1977, 36000, 32000
 - 1105, 60, 1978, 38000, 33000
 - 2634, 60, 1976, 34000, 30000
 - 2635, 60, 1977, 37000, 33000
 - 2636, 60, 1978, 40000, 35000

If I wanted to grab the 18 most recent entries for a particular year, how would I go about doing this? It also needs to be in reverse order, so that when I chart it the most recent year is on the right. I won’t necessarily know the most recent year, and it may not be the current year (most likely won’t be the current year).

I have a SQL statement in my php that looks like this:

$sql = "SELECT * FROM `values` WHERE `object_id`='$id' 
    ORDER BY  `year` DESC, `id` DESC LIMIT 18"
$result = $mysqli->query($sql);

This seems to get the right values, but puts them in the wrong order. I can simply go through the list in a reverse loop, but I’d like to know if there is a SQL statement that can do this for me.

Thanks!

Edit: I have tried the SQL with ORDER BY year ASC, id DESC and ORDER BY year DESC, id ASC. Neither of these work. The first still spits them out backwards, while the second spits them out from the first year instead of the last.

  • 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-05T14:47:07+00:00Added an answer on June 5, 2026 at 2:47 pm

    Add an outer select and flip the order on year:

    SELECT * FROM (
      SELECT year, col2, col3 ... FROM `values` WHERE `object_id`='$id' 
          ORDER BY  `year` DESC, `id` DESC LIMIT 18
      ) a
    ORDER BY YEAR ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have table with column 'URL' whrere I store urls like this
Let's say that I have a set of relations that looks like this: relations
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a text file composed like this ##### typeofthread1 ##### typeofthread2
Let's say I have some json like this in mongo: {n:5} and a java
Let's say I have a table with a Color column. Color can have various
Let's say I have a table called messages with the columns: id | from_id
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I can call a method like this: core::get() . What is the

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.