Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
select most recent record
Old 04-13-2010, 03:00 PM select most recent record
Extreme Talker

Posts: 173
Trades: 0
I am trying to list all projects and where there is a record in the status table, I want to pull in this data. However, in the status table there might be several entries, so I only want the latest.

tbl_project
project
A
B
C

tbl_status
project Status timestamp
A Good jan 1
A Bad Feb 1
B Good Mar 1

Require Output
project Status timestamp
A Bad Feb 1
B Good Mar 1
C

SELECT tbl_project.project, tbl_status_archive.*
FROM tbl_project LEFT JOIN tbl_status_archive ON tbl_project.project_id = tbl_status_archive.project_id;

I can't figure out how to isolate the project A

What I currently get

project Status timestamp
A Good jan 1
A Bad Feb 1
B Good Mar 1
C
dgkindy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 04-13-2010, 03:13 PM Re: select most recent record
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
http://www.w3schools.com/sql/sql_orderby.asp


getting only one record will depend on what database server you are using.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-13-2010, 03:20 PM Re: select most recent record
Extreme Talker

Posts: 173
Trades: 0
I am using phpMySQL
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-13-2010, 03:23 PM Re: select most recent record
Extreme Talker

Posts: 173
Trades: 0
ORDER BY only sorts the list in a given order. I am looking to remove the duplicate values only showing the last entry unless of cause the there is not record in which case the parent record should be displayed showing blank as a response for the items in subtable
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-13-2010, 03:25 PM Re: select most recent record
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
PHP is a programming language, MySQL is the database server phpmysql doesn't exist

Code:
SELECT fieldlist FROM table ORDER BY DATE LIMIT 1;
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-13-2010, 04:34 PM Re: select most recent record
Extreme Talker

Posts: 173
Trades: 0
I tried as you suggested but what I got was only one record. Played around a bit more and came up with this "

'SELECT tbl_project.*, tbl_status_archive.* '.
'FROM tbl_project LEFT JOIN tbl_status_archive ON tbl_project.project_id = tbl_status_archive.project_id '.
'GROUP BY tbl_project.project_id ';"

However, it returns the first record that is comes to in the child table rather then the most recently update record. So I am still struggling to find the the record from the child table that was most recently updated.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-13-2010, 05:43 PM Re: select most recent record
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
Originally Posted by dgkindy View Post
ORDER BY only sorts the list in a given order. I am looking to remove the duplicate values only showing the last entry unless of cause the there is not record in which case the parent record should be displayed showing blank as a response for the items in subtable
You can't cover all those criteria easily.

A JOIN will have duplicates, and a LEFT or RIGHT JOIN will leave out records with no match.

subqueries is probably the way to
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 04-13-2010, 10:14 PM Re: select most recent record
Extreme Talker

Posts: 173
Trades: 0
Okay, I have been trying to figure this out. So to your suggestion, I am simplifying.

project Status timestamp
A Good jan 1
A Bad Feb 1
B Good Mar 1



SELECT * FROM `tbl_status_archive` ORDER BY `date_stamp` DESC LIMIT 1

So I tried this statement and the result was as follows:

project Status timestamp
B Good Mar 1

I would like it to show

project Status timestamp
A Bad Feb 1
B Good Mar 1

Of course, I cannot just say limit two as I do not know how may records could be available.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-14-2010, 10:23 PM Re: select most recent record
Extreme Talker

Posts: 173
Trades: 0
Alright, got it working but not the way I was expecting and perhaps this is what chrishirst was suggesting.

Essentially, I just walked through the first list and each time I would query the second table using the limit to restrinct the result to only one record.
dgkindy is offline
Reply With Quote
View Public Profile
 
Old 04-15-2010, 10:23 AM Re: select most recent record
Junior Talker

Posts: 4
Name: rajiv kumar
Trades: 0
Hi ,

From your Question I understood AS This is the Result You Expect


Each Project Name,And Its Latest Status, And Its Latest Date

Ex Result:
A Bad Feb 1
B Good Mar 1
C

So try the below ,

SELECT
tbl_project .project , tableOfProjectAndLatestTmpStmp.*
FROM tbl_project LEFT JOIN
(
SELECT project, MAX(timestamp) FROM tbl_status
GROUP BY project
) tableOfProjectAndLatestTmpStmp

ON tbl_project .project= tableOfProjectAndLatestTmpStmp.project

thanks,
Rajiv kumar .B
rajiv kumar B is offline
Reply With Quote
View Public Profile
 
Old 04-15-2010, 10:34 AM Re: select most recent record
Junior Talker

Posts: 4
Name: rajiv kumar
Trades: 0
hi

I m sorry ,My previous query does Not include Status column,

Please find the below corrected Query

SELECT
tbl_project .project , tableOfProjectAndLatestTmpStmp.TmpStamp,
FROM tbl_project LEFT JOIN
(
SELECT project, MAX(timestamp) AS [TmpStamp] FROM tbl_status
GROUP BY project
) tableOfProjectAndLatestTmpStmp
LEFT JOIN
(
SELECT project, timestamp, status FROM tbl_status
)tableOfProjectAndStatus

ON tbl_project .project= tableOfProjectAndLatestTmpStmp.project AND
tableOfProjectAndLatestTmpStmp .project = tableOfProjectAndStatus.project AND
tableOfProjectAndStatus.timestamp = tableOfProjectAndLatestTmpStmp.[TmpStamp]
rajiv kumar B is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to select most recent record
 

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.53866 seconds with 12 queries