I have an Acccss Database which contains details about photos which belong to different albums. I’m trying to select the information about the album and grab the first photo that belongs to that album to use as a cover photo. If no photo has been added to the album I use a default image.
Tables
Albums
AlbumID PK
Description
DateModified
Photos
PhotoID PK
AlbumID FK
Filepath
This is the code I have so far, where am I going wrong?
SELECT
Alb.AlbumID,
Alb.[Description],
Alb.[datemodified],
IIF( Pho.FilePath IS NULL,
'AwaitPhoto.jpg',
Pho.FilePath ) AS FilePath
FROM Albums as Alb
LEFT OUTER JOIN Photos as Pho
ON (Pho.AlbumID = Alb.AlbumID
AND Pho.PhotoID =
(SELECT MIN(PhotoID) FROM Photos WHERE AlbumID = Alb.AlbumID )
)
of course i knew it was Access SQL (ptui) – the square brackets are microsoft only, and SQL Server has no IIF function
i just forgot one level of parentheses
SELECT Alb.AlbumID
, Alb.[Description]
, Alb.[datemodified]
, IIF( Pho.FilePath IS NULL
, 'AwaitPhoto.jpg'
, Pho.FilePath ) AS FilePath
FROM [COLOR="Red"][B]([/B][/COLOR]
Albums as Alb
LEFT OUTER
JOIN ( SELECT AlbumID
, MIN(PhotoID) as FirstID
FROM Photos
GROUP
BY AlbumID ) AS Fid
ON Fid.AlbumID = Alb.AlbumID
[COLOR="Red"][B])[/B][/COLOR]
LEFT OUTER
JOIN Photos as Pho
ON (
Pho.AlbumID = Alb.AlbumID
AND Pho.PhotoID = Fid.FirstID
)
Syntax error(missing operator) in Query Expression ‘Fid.AlbumID = Alb.AlbumID LEFT OUTER JOIN Photos as Pho ON ( Pho.AlbumID = Alb.AlbumID AND Pho.PhotoID = Fid.FirstID’
um… remove the parentheses, remove one of the condition, then save it, open it up in design view, and graphically add back the second join condition (it will add parentheses automatically)
SELECT Alb.AlbumID
, Alb.[Description]
, Alb.[datemodified]
, IIF( Pho.FilePath IS NULL
, 'AwaitPhoto.jpg'
, Pho.FilePath ) AS FilePath
FROM Albums as Alb
LEFT OUTER
JOIN ( SELECT AlbumID
, MIN(PhotoID) as FirstID
FROM Photos
GROUP
BY AlbumID ) AS Fid
ON Fid.AlbumID = Alb.AlbumID
LEFT OUTER
JOIN Photos as Pho
ON (
Pho.AlbumID = Alb.AlbumID
AND Pho.PhotoID = Fid.FirstID
)