473,395 Members | 1,516 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,395 software developers and data experts.

select last 4 records added to db but not show the last one

139 100+
Hello,

This is going to sound strange but I have a webpage that displays a form showing the last record added. This is because further records share a lot of the same data, so only a few fields have to be changed. I use the max(ID) to do this:

Expand|Select|Wrap|Line Numbers
  1. <cfquery name="lastid" datasource="swwdrawings">
  2. select        *
  3. from        tblDrawingsData
  4. where        ID = (select max(ID) from tblDrawingsData where AddedBy = '#username#')
  5.     </cfquery>
  6.  
I also have a side bar that shows the last 5 records so these can be easily editable (a link fires the ID of the record to the form):

Expand|Select|Wrap|Line Numbers
  1. <cfquery name="lastfive" datasource="swwdrawings" maxrows="5">
  2. select        *
  3. from        tblDrawingsData
  4. where        AddedBy = '#username#'
  5. order by    ID desc
  6. </cfquery>
  7.  
Obiviously this also shows the last (max) ID which already appears in the main form. How do I show the penultimate 5 records before the last one to be added? I have tried this but it doesn't appear to work:

Expand|Select|Wrap|Line Numbers
  1. <cfquery name="lastfive" datasource="swwdrawings" maxrows="5">
  2. select        *
  3. from        tblDrawingsData
  4. where        AddedBy = '#username#' and ID <> (select max(ID) from tblDrawingsData where AddedBy = '#username#')
  5. order by    ID desc
  6. </cfquery>
  7.  
Thanks!
Neil
Feb 14 '11 #1
4 3602
acoder
16,027 Expert Mod 8TB
Which database are you using? You could use limit in MySQL.

Another alternative is to take the ID returned by the first query and use it in the second query.
Feb 15 '11 #2
ndeeley
139 100+
I'm using a bog standard Access database. Will limit work with that? Must admit I have no idea how to use it!
Feb 15 '11 #3
acoder
16,027 Expert Mod 8TB
In Access, you can use select top 5. This will grab the first 5 (descending, so it'd be the last 5), but if you exclude the last one, then it'd be the penultimate 5. In your main query where you get the last record, store the ID to use in this query.
Feb 15 '11 #4
ndeeley
139 100+
Thanks acoder - I`ll give it a go!

Neil
Feb 15 '11 #5

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

Similar topics

0
by: M Wells | last post by:
Hello All, I'm wondering if anyone can tell me how to write an SQL statement that will select all records that are less than 10 minutes old, using a datetime column? Any help appreciated! ...
6
by: Christo | last post by:
I have this script for showing news on a page, but i want it to only show the last 10 records, as in the 10 records that were added to the database last. the script shows the entries in descending...
4
by: Tomas | last post by:
I'm creating MS Access database and I need to have in query an average of 3 last records. How to do it? Maybe here is some function or sql expresion? Thanks
1
by: jith87 | last post by:
hi, i am importing a text file into oracle database using sql loader.i need to ommit the first and last records of the text file while importing... can anybody help???? this is my text file... ...
1
by: premdasp | last post by:
Dear Sirs, I tried your valuable commands (below mentioned) but I got last record. Private Sub Form_Current() Dim rs As Recordset Set rs = .Form.RecordsetClone If rs.RecordCount Then...
9
paulrajj
by: paulrajj | last post by:
hi everybody, i am newbie to php and mysql. i have a little bit knowledge about php with xml. how to insert and select the records from xml to mysql using php?
2
by: dieselrocks | last post by:
Hopefully someone can shed some light on this. I have a table in a DB that is linked to another DB I deleted several records in the linked table. They no longer show up in any of the tables, but...
1
by: rchaitanya | last post by:
i want a command that will sleep for 10sec and show last 100 lines in unix log file i am using tail -f -s10 -n100 filename (this command showing 100 lines only once, but as the file is...
1
by: sareeka | last post by:
mean if we select mango pic: it show in selectedpicbox:, if we select orangepic: it show in selectedpicbox , if we select apple pic: it show in selectedpicbox
1
by: sentimental37 | last post by:
Hello everybody, I am developing a c# application in which i am using access2007 database. I want to select random records from the database through an access query in c#.but i am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.