douhuan1950 2014-07-16 13:56
浏览 9

Laravel是最受欢迎的产品

Im having 2 tables one with products and one favourit to keep it simple i show the relevant rows.

table poducts

id


table favourit

user_id not needed in query

product_id


What i want is find the top 10 of products wich are most common in the favourit table. I think i have to do

   favourit::groupBy(product_id) 

and

   favourit::orderBy(ammount(sum of all product_ids), desc)

But how can i get the ammount(sum of all product_ids) to order by? Also would this query (with a groupby) be possible to be joined with the products table so i get the products in one query?

  • 写回答

2条回答 默认 最新

  • dtamho6404 2014-07-16 14:14
    关注

    From an SQL point of view, this is how you would get the top 10 products from the favorit table:

    Select TOP 10
        Product_id
        ,COUNT(Product_id) as [Count]
    From
        favorit
    Group By
        Product_id
    Order By
        COUNT(Product_id) desc
    

    This will give you the top 10 Product_id's and sort them with the highest count on top.

    I am not too familiar with PHP so I wouldn't be able to tell you how to implement it, sorry :(

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算