MySql中两个表的数据计数总和

时间:2019-07-08 10:44:33

标签: mysql

我想要两个表中列的总数。

我正在尝试执行此操作,但是它给出了一张表的总数,但是我希望从bot中获得这些表的总和

select DISTINCT(video_id), 
    (select count(*) as cnt from votes 
        where video_id = voteable_id 
        AND votes.type = "up") as order_col
from video_views  
ORDER BY `order_col`  DESC**

视频观看次数表

video_id

    137
    136
    134
    137
    136
    137

投票表

   Voteable_id                     type

    137                             up
    137                             up
    134                             down
    135                             up
    134                             up
    134                             up

其结果应该是

video_id   count
137        5
134        3
136        2
135        1

1 个答案:

答案 0 :(得分:3)

您可以在下面尝试-

select vedio_id,sum(cnt)
from
(
select video_id, count(*) as cnt from Video_views
group by video_id
union all
select Voteable_id, count(*) from Votes where votes.type = "up"
group by Voteable_id
)A group by vedio_id