Jump to content

[SOLVED] Join - is it possible?


itchyfeets

Recommended Posts

Hi,

I have had a hard time searching for clues on this, but maybe someone can help? I'm alright with basic MYSQL statements but this is beyond me:

 

I have two tables I'd like to join but not in a straight forward manner.

 

Table: spreads

id (int, auto inc)

thumb_asset (int)

medium_asset (int)

large_asset(int)

 

Table: assets

id (int, auto inc)

path (varchar)

 

So the idea is that a spread has three asset paths and I'd like to output a list of each spread's asset paths like so:

 

spread 1: foo.gif, bar.jpg, moo.gif

spread 2: bar.jpg, foo.gif, moo.gif

 

But without doing loads of SELECTs in a PHP loop.

 

It seems like someone who knows MYSQL would find this easy - please help!

Link to comment
Share on other sites

something like this (Assuming the thumb_asset, medium_asset and large_asset are linking to the second table via Primary->foreign id relationship)

SELECT
spreads.id as row_id,
thumb.path as thumb_path,
medium.path as med_path,
large.path as large_path

FROM 
`spreads`

LEFT JOIN `assets` as thumb ON(spreads.thumb_asset = thumb.id)
LEFT JOIN `assets` as medium ON(spreads.medium_asset = medium.id)
LEFT JOIN `assets` as large ON(spreads.large_asset = large.id)

GROUP BY spreads.id

 

Link to comment
Share on other sites

I don't see the purpose of the group by, since it's on an ID field, and will do nothing but slow down the query.  And if it were on a non-unique field, the non-group'ed by fields would be meaningless.  If you were running this in MySQL 5 with strict mode, mysql would complain.

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.