LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-02-2011, 03:46 AM   #1
dexznrl
Member
 
Registered: Jul 2009
Posts: 36

Rep: Reputation: 0
Mysql query problem


Hello my fellow programmers,

I'm not one of the good ones at this so I could really need som help.

I have a table that I use as a log for a system.

In this log I have the columns: id, fname, lname, number, status and date.
Date is auto generated with date and time.

The column "id" is not unique.

The table is full of data and some id's have multiple status.
Short simplified example below:
id status date
5 return 20110101
5 return 20110102
5 no answer 20110103
5 return 20110104
5 no answer 20110105
6 return 20110101
6 return 20110102
6 no answer 20110103
6 return 20110104
6 no answer 20110105
6 return 20110106

Now I need to search the entire table for every "id" where the latest date for each "id" resulted in a "no answer".

From my short table example above I must get the results:
id status date
5 no answer 20110105
6 no answer 20110105
 
Old 03-02-2011, 06:21 AM   #2
kuyalfinator
Member
 
Registered: Jul 2003
Location: Los Angeles
Distribution: RedHat8 2.4.18-14 RedHat9 2.4.20-8 FreeBSD 5.2.1 Ubuntu 10_4 Mandrake 2.4.21-0.13mdk
Posts: 118

Rep: Reputation: 16
Let's start with the basic SQL.

Select id from your table
 
Old 03-02-2011, 07:00 AM   #3
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,261

Rep: Reputation: 315Reputation: 315Reputation: 315Reputation: 315
Use "GROUP BY id" and "MAX(date)".
 
Old 03-02-2011, 07:12 AM   #4
dexznrl
Member
 
Registered: Jul 2009
Posts: 36

Original Poster
Rep: Reputation: 0
Thanks to eSelix

That solved the problem.
 
Old 03-02-2011, 07:20 AM   #5
kuyalfinator
Member
 
Registered: Jul 2003
Location: Los Angeles
Distribution: RedHat8 2.4.18-14 RedHat9 2.4.20-8 FreeBSD 5.2.1 Ubuntu 10_4 Mandrake 2.4.21-0.13mdk
Posts: 118

Rep: Reputation: 16
Quote:
Originally Posted by eSelix View Post
Use "GROUP BY id" and "MAX(date)".
Hey eSelix, usually when learning SQL, I tend not to give answers. It defeats the purpose of learning.
 
Old 03-02-2011, 05:32 PM   #6
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,261

Rep: Reputation: 315Reputation: 315Reputation: 315Reputation: 315
Yes, I understand and promote that, not only with SQL. I refrained before giving whole query, this was only a hint, as I stated that your answer "SELECT id" was too general.
 
Old 03-02-2011, 07:29 PM   #7
kuyalfinator
Member
 
Registered: Jul 2003
Location: Los Angeles
Distribution: RedHat8 2.4.18-14 RedHat9 2.4.20-8 FreeBSD 5.2.1 Ubuntu 10_4 Mandrake 2.4.21-0.13mdk
Posts: 118

Rep: Reputation: 16
Ah, good job. I thought he was going to use a where clause, but group by and max worked too. There is more then one way to write the sql query he wants. The smaller the query, the better.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
remote access with mysql query browser problem Azalar Linux - Server 1 10-27-2007 03:55 PM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 07:36 PM
php/,mysql problem: can't query JJX Linux - General 4 01-06-2005 06:10 PM
Problem with SQL query using MySQL gigglesnorter Programming 2 04-22-2004 05:11 PM
Mysql/PHP query problem with datetime field. Pcghost Programming 2 11-11-2003 01:24 PM

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

All times are GMT -5. The time now is 07:10 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration