2013-10-12 197 views
0

时retrive所有数据与每个项目的计数我有两个表:连接两个表

Table 1: contest_video 

Table Rows: cv_id, uid, cid, cn_id, video_name, video_detail, video, image, cverify, date 

Table 2: contest_vote 

Table Rows: vid, cv_id, voter_id, date 

现在我想加入这两个表将与计数。像视频(视频名称)的票(VID)

总数我已经试过这样:

SELECT * 
FROM (`contest_video`) 
    LEFT JOIN `system_user` 
    ON `system_user`.`id` = `contest_video`.`uid` 
    LEFT JOIN `contest_vote` 
    ON `contest_vote`.`cv_id` = `contest_video`.`cv_id` 
WHERE `contest_video`.`cid` = '1' 
ORDER BY `contest_video`.`created_date` DESC 

但它不计数仅返回数据。请需要专家帮忙。

回答

0

你可以像下面这样做

使用组和列名,而不是*

SELECT 
    cv_id, 
    uid, 
    cid, 
    cn_id, 
    video_name, 
    video_detail, 
    video, 
    image, 
    cverify, 
    date, 
    count(`contest_video`.`cv_id`) Total 
FROM (`contest_video`) 
    LEFT JOIN `system_user` 
    ON `system_user`.`id` = `contest_video`.`uid` 
    LEFT JOIN `contest_vote` 
    ON `contest_vote`.`cv_id` = `contest_video`.`cv_id` 
WHERE `contest_video`.`cid` = '1' 
GROUP BY `contest_video`.`video_name` 
ORDER BY `contest_video`.`created_date` DESC