albums
表是aid | name | description | privacy | defaultpic | date | uid
和photos
表是id | date | uid | file | description | size | height | width | mime | aid | deleted
$AlbumListSQL = mysql_query("SELECT aid, defaultpic FROM albums WHERE $privacycheckSQL AND uid='$userid' LIMIT $start , $limit");
选择相册,无论相册中是否有照片。
$privacycheckSQL
可以是privacy='0'
或(privacy = '0' OR privacy ='1' OR privacy ='2')
或(privacy = '0' OR privacy ='1' OR privacy ='2')
SELECT COUNT(id) FROM photos WHERE deleted=0 and uid='$userid' AND aid='$aid'
返回相册的照片计数。我可以以某种方式将其写入一个查询中,该查询将返回其中包含超过零张照片的相册吗?我在想某种类型的联接或嵌套查询。我不确定。我正在使用5.1.54如果这意味着任何东西。
最佳答案
使用JOIN只会将在照片表中具有匹配记录的相册带入,Group By将仅对每个相册返回一条记录。
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
关于php - 只选择其中有照片的相册,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6810877/