Bluehost.com Web Hosting $6.95

SQL join but limit to 1 link??

This is a discussion on SQL join but limit to 1 link?? within the MySQL Database forums, part of the Database Forums category; Hey all, I have the following MySQL statement: SELECT tree.id, tree.type, tree.size, tree_pic.filename FROM tree, tree_pic ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-08-2007
andy
 
Posts: n/a
Default SQL join but limit to 1 link??

Hey all,

I have the following MySQL statement:

SELECT tree.id, tree.type, tree.size, tree_pic.filename
FROM tree, tree_pic
WHERE
AND tree.id=tree_pics.treeid
AND tree.type="oak";

I want to show the details of the tree with 1 picture.

this works ok if there is 1 picture linked to 1 tree, when I have more
than 1 picture linked to 1 tree I get duplicate records with the
picture changing each time.

Say the database contains 2 tree records and 3 picture records, 1 tree
links to 1 picture and 1 tree links to 2 pictures.

I would get:

Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg
Tree Id:2 Type:Oak Size:2m Filename: tree2-pic2.jpg

How do I limit to only 1 picture per tree record so I would get:

Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg

Thanks

Reply With Quote
  #2 (permalink)  
Old 03-08-2007
Paul Lautman
 
Posts: n/a
Default Re: SQL join but limit to 1 link??

andy wrote:
> Hey all,
>
> I have the following MySQL statement:
>
> SELECT tree.id, tree.type, tree.size, tree_pic.filename
> FROM tree, tree_pic
> WHERE
> AND tree.id=tree_pics.treeid
> AND tree.type="oak";
>
> I want to show the details of the tree with 1 picture.
>
> this works ok if there is 1 picture linked to 1 tree, when I have more
> than 1 picture linked to 1 tree I get duplicate records with the
> picture changing each time.
>
> Say the database contains 2 tree records and 3 picture records, 1 tree
> links to 1 picture and 1 tree links to 2 pictures.
>
> I would get:
>
> Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic2.jpg
>
> How do I limit to only 1 picture per tree record so I would get:
>
> Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg
>
> Thanks


Do a search on this group for "strawberry query"


Reply With Quote
  #3 (permalink)  
Old 03-08-2007
Captain Paralytic
 
Posts: n/a
Default Re: SQL join but limit to 1 link??

On 7 Mar, 23:11, "andy" <jenki...@gmail.com> wrote:
> Hey all,
>
> I have the following MySQL statement:
>
> SELECT tree.id, tree.type, tree.size, tree_pic.filename
> FROM tree, tree_pic
> WHERE
> AND tree.id=tree_pics.treeid
> AND tree.type="oak";
>
> I want to show the details of the tree with 1 picture.
>
> this works ok if there is 1 picture linked to 1 tree, when I have more
> than 1 picture linked to 1 tree I get duplicate records with the
> picture changing each time.
>
> Say the database contains 2 tree records and 3 picture records, 1 tree
> links to 1 picture and 1 tree links to 2 pictures.
>
> I would get:
>
> Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic2.jpg
>
> How do I limit to only 1 picture per tree record so I would get:
>
> Tree Id:1 Type:Oak Size:1m Filename: tree1-pic1.jpg
> Tree Id:2 Type:Oak Size:2m Filename: tree2-pic1.jpg
>
> Thanks


Hmm, I thought I posted to this last night!
Oh well, seciond attempt:
Search this group for "strawberry query"

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




All times are GMT +1. The time now is 03:05 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0