Forum Moderators: open

Message Too Old, No Replies

SQL/ classic asp issue

SQL, joins, asp, photos

         

KRMwebdesign

1:42 pm on Apr 27, 2011 (gmt 0)

10+ Year Member




Hi All,
I am trying to put together a number of photo albums. The photo albums show up as images on a page using a repeat region. When you click on the photo album image that will show the photos in the album.

I have set up 2 tables Photos and PhotoAlbums. Each record in the Photos table has an PHotoAlbumID to link it to the photo album.

What I want is to show the first image in the album for each photo album cover. So if the first imageID is 6 then image 6 should show up on the photo albums page as the Album cover if you understand what I mean?

This is the SQL statement I have at the moment but I know it won't do what I want:
"Select * FROM PhotoAlbums left join Photos on PhotoAlbums.AlbumID = Photos.AlbumID ORDER BY Photos.AlbumID asc"

I wonder can anyone enlighten me on what statement I could be putting here?

kb5nju

3:21 pm on Apr 27, 2011 (gmt 0)

10+ Year Member



Are you trying to grab just the first photo assigned to each album? If that's the case, you may have to loop through the AlbumID numbers in that table and pull each one separately.

Something like this might work in that case:

SELECT TOP 1 *
FROM Photos, PhotoAlbums
WHERE Photos.AlbumID = PhotoAlbums.AlbumID AND PhotoAlbums = 1

KRMwebdesign

8:15 am on Apr 28, 2011 (gmt 0)

10+ Year Member



Thanks for the reply kb5nju.
I will try this and see what happens. Thanks again.

KRMwebdesign

10:41 am on Apr 28, 2011 (gmt 0)

10+ Year Member



I keep getting this error:

Microsoft JET Database Engine error '80040e10'

No value given for one or more required parameters

kb5nju

1:35 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



Yup - I forgot a part. Fix the WHERE clause to end like this:

AND PhotoAlbums.AlbumID = 1

KRMwebdesign

1:43 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



I should have seen that myself tbh :). Thanks for the help. I'll try it again.

Many thanks

KRMwebdesign

3:43 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



Hi again,
I have tested this select statement on my page "select * from PhotoAlbums left join Photos on PhotoAlbums.AlbumID = Photos.AlbumID ORDER BY Photos.picID desc" and it is working okay. There is only 1 problem with it. It is taking every picture in the "Photos" table and showing them as an Album cover but I am just looking for 1 image out of each album to be the album cover. Does anyone know how I can achieve this?

I didn't get any results for the above using this statement "SELECT TOP 1 *
FROM Photos, PhotoAlbums
WHERE Photos.AlbumID = PhotoAlbums.AlbumID AND PhotoAlbums = 1 " I'm afraid.

Demaestro

4:13 pm on Apr 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



you can simply add a limit clause to the end of the query

select * from PhotoAlbums left join Photos on PhotoAlbums.AlbumID = Photos.AlbumID ORDER BY Photos.picID desc limit 1

kb5nju

4:53 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



"limit 1" - that's a new one on me. Thanks.

KRMwebdesign

6:38 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



I am using MsAccess database at the moment and "Limit 1" isn't working. I plan to migrate to MySQL but not until I develop the software properly first. Any other suggestions?

KRMwebdesign

6:42 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



Okay, I have this statement working "Select Top 1 * from PhotoAlbums left join Photos on PhotoAlbums.AlbumID = Photos.AlbumID ORDER BY Photos.picID desc" but it is only returning the first photo album and not the rest. Is there anything I can do to fix this?

Demaestro

7:02 pm on Apr 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Try removing the TOP 1... that is telling it to only return 1

KRMwebdesign

7:19 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



Okay but if I do that it returns every photo as a photo album but not every album with an image from the photos table :s

kb5nju

7:37 pm on Apr 28, 2011 (gmt 0)

10+ Year Member



KRM - you will need to write a query in to pull out all the AlbumID records from that table, then loop through that list and pull the first picture out of each Album running the query you just got working for each number in the list.

Demaestro

7:47 pm on Apr 28, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



kb5 is right. You need 2 queries and a loop

pseudo code looks like this:

sql = "SELECT * FROM Photos"

photo_data = sql_execute(sql)

for (photo in photo_data) {
cover_sql = "Select Top 1 * from PhotoAlbums where PhotoAlbums.AlbumID = " photo['AlbumID']

cover_data = sql_execute(cover_sql)

print cover_data[0][0]
}

mrmobility

1:48 pm on Jun 9, 2011 (gmt 0)

10+ Year Member



Could you do something like this?

SELECT * FROM
PhotoAlbums
INNER JOIN
(SELECT MIN(ImageId) AS ImageId,AlbumID
FROM Photos
GROUP BY AlbumId) FirstImages
ON
(FirstImages.AlbumId = PhotoAlbum.AlbumId)
INNER JOIN
Photos
ON
(FirstImages.ImageId = Photos.ImageId)