我有一个表,其中包含有关用户订阅的详细信息(id、user_id、plan_id、product_id、amount..等)。我想通过在表列中获得id (plan_id, product_id)
的最大出现次数来获得最喜欢的计划和产品。
我试着用这个在SQL列中查找最频繁的值。但没有成功。
以下是我对最喜欢的计划的查询:
$subs = Subscription::findBySql("SELECT plan_id, COUNT(plan_id) AS `value_occurrence` FROM subscription GROUP BY plan_id
ORDER BY `value_occurrence` DESC LIMIT 1");
我只需要plan_id
和product_id
的最大出现次数。
谢谢你的帮助。
我认为您需要以这种方式计算plan_id和product_id的出现次数(我认为您的模型称为Subscription):
$plantCounts = Subcription::find()
->select(['COUNT(*) AS value_occurrence'])
->orderBy(['value_occurrence'= DESC ]
->groupBy(['plan_id'])
->one();
从任何一行,即使是作为源的不同行,您想要plan_id的最大值和product_id的最小值吗?总共1排回来了?
select max(plan_id) as plmax,
max(product_id) as prodmax
from subscription