2013-10-12 201 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