Help with SQL Syntax

Hoping someone can help with a photo gallery migration...I'm not really proficient with SQL, but am 99% done.

I have two tables, PHOTOS and PHOTOS_ALBUMS

PHOTOS is essentially all the pictures in the gallery (id), along with id of the uploader (creator), and what Album those pictures belong to (albumid).

ALBUMS is all the albums (id), the user that created it (creator), and what Photo should be set as the thumbnail for the album (photoid)

I want to set the thumbnail for all the albums.  Some users have multiple albums.  I want the thumbnail to be the last photo they uploaded to each album.

So I need a query that will

A) Go through PHOTOS and select the last id from each albumid
B) In ALBUMS set photoid for each album to the last id selected above

Here are some sample tables:

PHOTOS

ID = 1
ALBUMID = 26
CREATOR = 9647
FILE: imagepath/81729.jpg

ID = 2
ALBUMID = 26
CREATOR = 9647
FILE: imagepath/0458435.jpg

ID = 3
ALBUMID = 35
CREATOR = 9647
FILE: imagepath/7347328974.jpg


ALBUMS

ID = 26
CREATOR = 9647
PHOTOID =

So in the example above, I'd want to populate alumbs.photoid with the last photos.id (in this case, 2).  Of course I have 100,000 users and 250,000 albums to do this for.

I know I could be clearer, but it's 3:30 AM here and my mind is fried...even I can barely make sense of what I am writing at this hour!
rovermediaAsked:
Who is Participating?
 
Bill PrewCommented:
Try this.

~bp
UPDATE albums a
SET    a.photoid = (SELECT   Max(p.id)
                    FROM     photos p
                    WHERE    a.albumid = p.albumid
                    GROUP BY p.albumid);

Open in new window

0
 
isaackhaziCommented:
Are you using any API or directly wanna do it in SQL?

I could give you a solution in .NET if you want.
0
 
brandscapeCommented:
a) SELEXT MAX(ID) FROM ALBUMS GROUP BY ALBUMID
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
isaackhaziCommented:
@brandscape: rovermedia wants to update all the albums with the last uploaded photo ID not just select em.
0
 
brandscapeCommented:
I would suggest using an update based on a select using table alises, something like this

update albums
set albums.photoid = max(sel.id)
from photos sel
group by sel.albumid
where albums.albumid = sel.albumid
0
 
brandscapeCommented:
sorry - my first post was to answer part A) but should read :

a) SELEXT MAX(ID) FROM PHOTOS GROUP BY ALBUMID

IT's usefull to see the answer to B) in context of the answer to A)
0
 
apexpertCommented:
SELECT MAX(ID) FROM PHOTOS GROUP BY ALBUMID
0
 
awking00Commented:
What dbms are you using?
0
 
Frans HaesendonckxFreelance ConsultantCommented:
Supposing you work with Microsoft SQL Server, the statement could be :

UPDATE Albums SET PHOTOID = b.maxid from
Albums a INNER JOIN (SELECT AlbumID , Max(ID) maxid FROM Photos group by AlbumID) b  
ON a.ID = b.ALBUMID

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.