mysql查询修改
您好,我已经编写了此查询
SELECT cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear,
SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed
FROM cl_doctor_call LEFT JOIN cl_doctor_call_cstm ON
cl_doctor_call.id=cl_doctor_call_cstm.id_c
LEFT JOIN cl_brands ON
CONCAT('^',cl_brands.id,'^') LIKE CONCAT(cl_doctor_call_cstm.brand_discussed_c, '%' )
WHERE (cl_doctor_call.date_entered)
BETWEEN CURDATE()-INTERVAL 3 MONTH AND CURDATE()
GROUP BY cl_brands.name,MonthYear
ORDER BY No_Times_Brand_Discussed DESC limit 1
此查询为我们提供了讨论的最高品牌以及品牌名称和月份年份。
例如:
Brand Monthyear No_Times_Brand_Discussed
x Nov11 5
现在我想显示品牌在过去 2 个月内讨论的“同一品牌”详细信息(这意味着我的查询应该返回过去 2 个月内相同品牌详细信息的结果) 所以我的结果会是这样的。
Brand Monthyear No_Times_Brand_Discussed
x Nov11 5
x Oct11 0
x Sep11 1
告诉我如何修改上面的查询,以便它给出这个结果。
Hi I have written this query
SELECT cl_brands.name AS Brand,DATE_FORMAT(cl_doctor_call.date_entered,'%b%y')AS MonthYear,
SUM(FIND_IN_SET(CONCAT('^',cl_brands.id,'^'),cl_doctor_call_cstm.brand_discussed_c))AS No_Times_Brand_Discussed
FROM cl_doctor_call LEFT JOIN cl_doctor_call_cstm ON
cl_doctor_call.id=cl_doctor_call_cstm.id_c
LEFT JOIN cl_brands ON
CONCAT('^',cl_brands.id,'^') LIKE CONCAT(cl_doctor_call_cstm.brand_discussed_c, '%' )
WHERE (cl_doctor_call.date_entered)
BETWEEN CURDATE()-INTERVAL 3 MONTH AND CURDATE()
GROUP BY cl_brands.name,MonthYear
ORDER BY No_Times_Brand_Discussed DESC limit 1
This query gives us highest brand discussed with brand name and monthyear.
Eg:
Brand Monthyear No_Times_Brand_Discussed
x Nov11 5
Now i want show the brand discussed details of ''same brand'' for last 2 months (that means my query should return me results for same brand details for more last 2 months)
so my result would be like that.
Brand Monthyear No_Times_Brand_Discussed
x Nov11 5
x Oct11 0
x Sep11 1
Tell me how can I modify my above query so that it will give me this result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没看错的话,基本上,您只需要获取讨论最多的品牌按日期排序的前 3 个结果即可。因此,您只需以相同的方式查询原始表,但包含一个“in”子句,将您限制为讨论最多的品牌。我会使用 ID 而不是名称字段,这样您就知道它是唯一的。我假设字段名称是“brand_id”,但您的表中可能有所不同。
If I'm reading this right, essentially, you just need to get the top 3 results ordered by date for the brand that is the most discussed. So, you just have to query your original tables the same way, but include an "in" clause that limits you to the brand that is most discussed. I'd use an ID instead of a Name field so you know it's unique. I'm assuming that field name is "brand_id" but it might be something different in your table.