473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to SELECT the most recent entry and the one before it?

106 100+
I have a table called news.
The table is some like that:

- id -- title -- article -- date -
- -
- - - - - - - - - - - - - - - - -

What I want to do is SELECT the most recent entry (not sure if Date field is necessary for that) and also the one before it.

So if I have this:


- id -- title -- article -- date -

1 -- News1 -- Article1 -- 1/1/1

2 -- News2 -- Article2 -- 2/1/1

3 -- News3 -- Article3 -- 3/1/1

4 -- News4 -- Article4 -- 4/1/1

- - - - - - - - - - - - - - - - - -

What I want is:
  • On day 1/1/1 the code gives the News 1
  • On day 2/1/1 the same code gives the News 2 and 1
  • On day 3/1/1 the " " " " News 3 and 2
  • On day 4/1/1 the " " " " News 4 and 3


Is it possible? How?

SELECT * from news WHERE ???

Please help me thanks.

==================

SOLVED

I've found the answer, it is posted below.

===================
Aug 1 '10 #1

✓ answered by Atli

What I want to do is SELECT the most recent entry (not sure if Date field is necessary for that) and also the one before it.
What if the most recent entry is not today? Your solution would not work if that is the case.

If you want the latest entries from a database, you can simply use the ORDER BY clause on an integer primary key that uses AUTO_INCREMENT. (Or something equivalent.)
For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM news
  2. ORDER BY id DESC
  3. LIMIT 2;
It basically just flips the table over and returns the top (previously bottom) 2 entries.

If you don't have a integer PK, then you would have to pick some other appropriate field. Such as a timestamp that stores the time when the row was created.

4 1871
TheServant
1,168 Expert 1GB
Please post your solutions to help other people who search for the same problem.
Aug 1 '10 #2
londres9b
106 100+
Here's how to solve the above problem:

Expand|Select|Wrap|Line Numbers
  1. SELECT * from news WHERE date between date_sub(curdate(), interval 1 day) and curdate();
  2.  
Hope this helps somebody as it helped me.
Aug 1 '10 #3
Atli
5,058 Expert 4TB
What I want to do is SELECT the most recent entry (not sure if Date field is necessary for that) and also the one before it.
What if the most recent entry is not today? Your solution would not work if that is the case.

If you want the latest entries from a database, you can simply use the ORDER BY clause on an integer primary key that uses AUTO_INCREMENT. (Or something equivalent.)
For example:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM news
  2. ORDER BY id DESC
  3. LIMIT 2;
It basically just flips the table over and returns the top (previously bottom) 2 entries.

If you don't have a integer PK, then you would have to pick some other appropriate field. Such as a timestamp that stores the time when the row was created.
Aug 2 '10 #4
londres9b
106 100+
@Atli
You're right thank you
Aug 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Brad Kent | last post by:
I have a problem that can be summarized with a simple table having the following 3 columns: timestamp userid status The table is constantly being updated. I'd like to get the most recent row...
3
by: TJM | last post by:
hello, I have a table which contains the audit time field. This audit time field records the current date and time when a record is inserted. I would like to output the most recent top 500 rows....
1
by: Tim Graichen | last post by:
Good morning, I have a sub-form that displays records from a table as a continuous form. The table has several hundred records, but the subform only displays five or six records. The records do...
2
by: Shaiguy | last post by:
I have a table containing the following fields: ProjectUpdateID (PrimaryKey) ProjectID UpdateDate I would like to create a Query in Ms Access 2000 which will return me the most recent 2...
3
by: manny | last post by:
Problem: how to have query show only most recent records. This query shows all exams in 2005 for particular individual (grades not shown to avoid embarrassing John Slacker!): SELECT...
11
by: jd_12345 | last post by:
On DB2 on AIX, I'm attempting to retrieve the most recent ID for each unique Code based on its EffectiveDate. For example, in the data below, I want to retrieve ID BE0191026 as it has the most...
4
by: Sector 7G | last post by:
I'm working with a SQL query for a Human Resources database. Its intended purpose is to find all the paycheck records with a check date (prckhist.chkdate ) more recent than eleven days past the...
2
by: robert.waters | last post by:
I need to perform the following: - select the most recent X number of records in a table (there is a timestamp field) - select the Nth occurrence of X number of records ex: - most recent 10...
1
by: jpgoeth | last post by:
What I'm trying to do is select the most recent record for each customer who created an account between 2004-01-01 and 2007-01-01. I'm using MSQuery. I have this so far: SELECT OrderOrAdjust,...
1
by: lutz | last post by:
I fully admit, I am begging for some help. I am at the mercy of anyone's generous nature. I wish I had a template to follow for this one. I thank you in advance for anyone's advice. Maybe...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.