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
https://forums.phpfreaks.com/topic/122063-solved-join-is-it-possible/
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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.