If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Help my brain on a select please !

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-12-09, 11:23
qazqaz qazqaz is offline
Registered User
 
Join Date: Aug 2009
Posts: 1
Help my brain on a select please !

Hi I am hoping some of you nice poeple can spend a few mins to help me out.

example:

table 1: id, name

table 2: id,table_1_id, date

Table 2 holds multiple rows that link to table 1 on the fk table_1_id.

My result set wants to be include just one row from table 2 which holds the latest date - (or even the max(id) )

so if we have :

table1:

1, Jim

table 2:
1, 1, 01-01-2010
2,1, 01-01-2009
3,1, 01-01-2008

I just want to be able to get a 1:1 on the join , and get just the table2 row 1 back....


hope that makes sense?
Reply With Quote
  #2 (permalink)  
Old 08-12-09, 12:53
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
SELECT t1.id
     , t1.name
     , t2.id
     , t2.date
  FROM table1 AS t1
INNER
  JOIN ( SELECT table_1_id
              , MAX(date) AS max_date
           FROM table2
         GROUP
             BY table_1_id ) AS m
    ON m.table_1_id = t1.id
INNER
  JOIN table2 AS t2
    ON t2.table_1_id = t1.id
   AND t2.date = m.max_date
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On