Jump to content

GROUP BY with RIGHT JOIN problem


Paulkirkewalker

Recommended Posts

Hi there,

I have two tables: properties and images.  They are linked by the primary key of properties, property_id.  Some records in properties have more than one record in images associated with them  and some have none at all.
What I am trying to do is return all the records in properties, whether they have an associated record in images or not [i]but[/i] only return one record (it doesn't matter which) for those which have more than one record in images associated with them.

If I run:
[code]
SELECT images.*
FROM images
GROUP BY images.property_id
[/code]
I get just one record associated with each property.  Fine.  My query linking properties and images runs fine too:
[code]SELECT properties.*, images.*
FROM images RIGHT JOIN properties ON images.property_id = properties.property_id;
[/code]

As expected, it returns every record in properties along with associted records in images whether any exist or not.  The problem comes when I try to add the GROUP BY line to this query:
[code]
SELECT properties.*, images.*
FROM images RIGHT JOIN properties ON images.property_id = properties.property_id;
GROUP BY images.property_id
[/code]

I just get this error: #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 'GROUP BY images.property_id' at line 1

Can anyone help?  This is driving me mad!  I accept that my approach with these queries may be completely wrong to start with so I'd be very grateful for any advice.

Thanks,
Paul.


But when I add the same GROUP BY line to the end on
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.