LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2009, 01:56 AM   #1
ShaqDiesel
Member
 
Registered: Jul 2005
Posts: 144

Rep: Reputation: 15
sql statement question


I have a table that looks like this:
Code:
comment_id(int)|timestamp(timestamp)|comment(varchar)
I want to extract the latest comment whose comment column matches 'Include:%/%'

so if I have:
Code:
1  	2009-10-15 23:21:18  	Include:\ndir1/dir2/file1.java
2 	2009-10-15 23:18:21 	blah
3 	2009-10-15 23:22:15 	Include:\ndir1/dir2/file2.java
4 	2009-10-15 23:18:53 	dir1/dir2/dir3/file3.java
5 	2009-10-15 23:19:24 	blah blah
the query should return comment 3. I tried SELECT comment FROM comments WHERE timestamp=MAX(timestamp) GROUP BY 'Include:%/%' and it didn't work.
 
Old 10-16-2009, 04:47 AM   #2
guard
LQ Newbie
 
Registered: Jul 2009
Location: Minsk, Belarus
Distribution: Gentoo, Ubuntu
Posts: 17

Rep: Reputation: 0
SELECT * FROM comments WHERE comment LIKE "Include:%" ORDER BY timestamp DESC LIMIT 1
 
Old 10-16-2009, 07:40 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Another way which is similar to what you where trying to do - this works so long as timestamp is unique. It may go wrong if it's not :
Code:
SELECT * from comments where timestamp=(select max(timestamp) from comments where comment like "Include:%%");
Just another idea..although guard's solution was possibly cleaner.

Last edited by bgeddy; 10-16-2009 at 07:42 AM.
 
Old 10-17-2009, 01:26 AM   #4
guard
LQ Newbie
 
Registered: Jul 2009
Location: Minsk, Belarus
Distribution: Gentoo, Ubuntu
Posts: 17

Rep: Reputation: 0
Your idea is right and closer to the initial attempt, but I think the order and limit combination is recommended (I believe it would be better optimized by the DB engine).
Also note that your approach might not work if the timestamp is replaced with something like smalldatetime.
 
Old 10-17-2009, 07:52 AM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
The requirement was to select the latest record which contains a certain string
If the time stamp is the wrong format, not allowing this selection, or the time stamp is not unique the database design is flawed. You cannot omit storing certain data and expect to be able to retrieve it.

Guard's first solution seems simple and correct.

jlinkels
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL statement assembler for C++? ta0kira Programming 3 03-07-2009 02:16 AM
help optimizing this SQL statement hedpe Programming 1 05-30-2007 07:06 AM
help with simple sql statement sekelsenmat Programming 6 08-06-2005 12:01 PM
mysql sql statement help mrtwice Programming 4 12-02-2003 10:04 AM
SQL statement glj Programming 1 10-12-2001 09:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:33 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration