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 > Joining Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-04-06, 07:49
GoMo GoMo is offline
Registered User
 
Join Date: Nov 2003
Location: London, UK
Posts: 39
Joining Tables

Is it possible to join a single row (if any) from another table? even though there is more than one row in the joining table

Code:
tbl_task                 tbl_attachments                         
id    name               id    task_id    file
1     Fix Bug            1     1          screenshot.jpg
2     Test               2     1          trace_output.txt
3     Upload
it doesn't matter which attachment is shown, but as long as one is

Code:
tbl_task_attachment
id   name          file
1    Fix Bug       screenshot.jpg
2    Test          NULL
3    Upload        NULL
Reply With Quote
  #2 (permalink)  
Old 07-04-06, 08:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select t.id
     , t.name
     , a.file
  from tbl_task as t
left outer
  join tbl_attachments as a
    on a.task_id = t.id
   and a.id
     = ( select max(id)
           from tbl_attachments
          where task_id = t.id )
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 07-05-06, 07:50
GoMo GoMo is offline
Registered User
 
Join Date: Nov 2003
Location: London, UK
Posts: 39
No luck, here's the query I'm using...

Code:
SELECTtbl_subtask.id, tbl_subtask.name, tbl_attachment.attachment
FROM tbl_task
LEFT JOIN tbl_attachment ON tbl_attachment.subtask_id = tbl_subtask.id
AND tbl_attachment.id = (
SELECT max( id )
FROM tbl_attachment
WHERE tbl_attachment.subtask_id = tbl_subtask.id )
and I get the following error ...

Code:
#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select max(id)
           from tbl_attachment
          where
Reply With Quote
  #4 (permalink)  
Old 07-05-06, 08:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
upgrade to 4.1
__________________
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