开发者

select only albums with photos in them

开发者 https://www.devze.com 2023-03-22 10:12 出处:网络
albums table is aid | name | description | privacy | defaultpic | date | uid and photos table is id | date | uid | file | description | size | height | width | mime | aid | deleted

albums table is aid | name | description | privacy | defaultpic | date | uid and photos table is id | date | uid | file | description | size | height | width | mime | aid | deleted

$AlbumListSQL = mysql_query("SELECT aid, defaultpic开发者_StackOverflow FROM albums WHERE $privacycheckSQL AND uid='$userid' LIMIT $start , $limit");

selects albums no matter if they have photos in them or not. $privacycheckSQL can be privacy='0' or (privacy = '0' OR privacy ='1' OR privacy ='2') or (privacy = '0' OR privacy ='1' OR privacy ='2')

SELECT COUNT(id) FROM photos WHERE deleted=0 and uid='$userid' AND aid='$aid' returns the count of the photos for a album. Can i write this in to one query somehow that will return albums with over zero photos in them? I was thinking some type of join or nested query. I'm not sure.

I'm using 5.1.54 if that means anything.


Use a JOIN , which will only bring albums which have matching records in photos table, Group By will return only one record per album.

SELECT albums.aid, albums.defaultpic 
FROM albums JOIN photos on (albums.aid = photos.aid and photos.deleted = 0)
WHERE $privacycheckSQL AND albums.uid='$userid' 
GROUP BY aid
LIMIT $start , $limit
0

精彩评论

暂无评论...
验证码 换一张
取 消