0

我有两张桌子:

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

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

4

1 回答 1

0

你可以这样做

使用 group by 和列名而不是*

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
于 2013-10-12T11:25:20.587 回答