仅选择包含照片的相册

时间:2011-07-25 01:02:56

标签: php mysql

albums表格为aid | name | description | privacy | defaultpic | date | uidphotos表格为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'会返回相册的照片计数。我能以某种方式将此写入一个查询,以便返回包含超过0张照片的相册吗?我在考虑某种类型的连接或嵌套查询。我不确定。

如果这意味着什么,我正在使用5.1.54。

1 个答案:

答案 0 :(得分:2)

使用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