确定专辑的SQL查询和总视频

时间:2012-04-28 01:46:20

标签: php mysql union

对于我需要执行的查询,用户将视频上传到相册,视频页面,相册列表及其下方的视频总数。

目前,我要做的是联盟,但这不起作用,这是我的代码:

$query="SELECT albname, albcover, lid, NULL as vidcount FROM albums WHERE USERID='".mysql_real_escape_string($USERID)."' and verified='1'

UNION

SELECT NULL as albname, NULL as albcover, NULL as lid, count(*) as vidcount FROM albs_vids A LEFT JOIN albums B USING(ALBID) WHERE B.USERID='".mysql_real_escape_string($UID)."'";

那么,会发生什么情况是SQL查询需要从表albums中找到相册的数据,即albname,albcover和lid。然后在表albs_vids中找到与相册对应的视频数量。该相册在两个表格中均由ALBID指定。

1 个答案:

答案 0 :(得分:1)

很难完全理解您的要求,但这会为您提供包含您的用户ID的每张专辑的视频数量:

$sql = "SELECT a.albname, a.albcover, a.lid, COUNT(*) AS vidcount
FROM albums AS a
LEFT JOIN albs_vids AS b ON b.albid = a.albid
WHERE b.userid='".mysql_real_escape_string($UID)."'
GROUP BY a.albname";