使用两个表格和订单结果

时间:2015-07-15 15:33:16

标签: mysql

我有这个MySQL查询:

SELECT C.Id, C.like, COUNT(V.Id) as Count FROM des C, views V WHERE C.Id = V.Id_des order by C.like, Count DESC LIMIT 20"

正常工作以找到包含大量视图和喜欢的第一个最佳ID,但他只显示一行!我希望它能显示所有内容( DES 表格),但要从那些有很多观点+喜欢到最低点的那个。

DES 表格包含ID,文字,说明,所有项目信息(想要根据视图显示此行)

VIEWS 表包含项目的每个视图,每次用户查看它时,我们都会保存id_des(DES)+ ip地址。

我也做了同样的事情:

    SELECT 
        des.Id, 
        des.like, 
        COUNT(views.Id_des) as Count 
    FROM 
        des
        LEFT JOIN views ON views.Id_des = des.Id
    ORDER BY
        des.like, 
        Count DESC LIMIT 20

等待你的帮助,提前谢谢你。

1 个答案:

答案 0 :(得分:0)

添加GROUP BY des.Id

SELECT 
    des.Id, 
    des.like, 
    COUNT(views.Id_des) as Count 
FROM 
    des
    LEFT JOIN views ON views.Id_des = des.Id
GROUP BY des.Id
ORDER BY
    des.like,
    Count DESC 
LIMIT 20

通过COUNT个观看行+更喜欢订购:

SELECT 
    des.Id, 
    des.like, 
    COUNT(views.Id_des) as Count, 
    des.like + COUNT(views.Id_des) as Count2
FROM 
    des
    LEFT JOIN views ON views.Id_des = des.Id
GROUP BY des.Id
ORDER BY
    Count2 DESC 
LIMIT 20