我正在从我对 MySQL 数据库的查询中寻找一种特殊的要求,我想以不同的方式应用 order by。例如。在具有以下数据的 user_id 和 user_rating 和 user_department 字段的数据库中。
+------------------------------------------------------+
| user_id | user_rating | user_department |
+------------------------------------------------------+
| 1 | 102 | A |
| 2 | 33 | B |
| 3 | 43 | C |
| 4 | 54 | A |
| 5 | 63 | A |
| 6 | 214 | B |
| 7 | 82 | A |
| 8 | 87 | C |
| 9 | 43 | A |
| 10 | 98 | A |
| 11 | 73 | C |
| 12 | 31 | A |
+------------------------------------------------------+
鉴于上述结构,我想按照 each_department 的 user_rating 顺序对结果进行排序,我需要每个 user_department 最多 5 条记录,其中初始 3 条记录应该是按照他们的评分顺序,但其余 2 应该是随机的。
所以在上述情况下,输出将类似于:
+------------------------------------------------------+
| user_id | user_rating | user_department |
+------------------------------------------------------+
| 1 | 102 | A |
| 10 | 98 | A |
| 7 | 82 | A |
| 12 | 31 | A |
| 5 | 63 | A |
| 6 | 214 | B |
| 2 | 33 | B |
| 8 | 87 | C |
| 11 | 73 | C |
| 3 | 43 | C |
+------------------------------------------------------+
我尝试了 web 上可用的选项来自定义排序依据,例如使用 FIELD 函数,但在这里找不到那么有用。还尝试使用子查询解决它,但该选项看起来也不可行,因为 MySql 不允许我在查询中同时使用 IN 和 LIMIT 关键字。
有没有更好/更简单的方法来解决这个问题。
最佳答案
这不是您要找的东西,但它提供了一种方法。
想法是使用 group_concat() 将每个部门的前 5 个值放在一个列中。此列的形式为:
user_id:rating
最多重复五次,以逗号分隔。如:
1:182,10:98,7:82,12:31,5:63
执行此操作的查询是:
select user_department,
substring_index(group_concat(concat(user_id, ':', user_rating)
order by user_rating desc
), ',', 5)
from t
group by user_department;
这不处理最后两个值的随机化。它把所有东西都排成一排。但是,我认为这可能会有所帮助。
我的下一次尝试技术上会按照您的要求进行,但它冒着最后两个“随机”用户可能相同的风险。
它使用与上面相同的 group_concat() 技巧。然而,它通过使用 substring_index() 从列表中选择不同的值来超越:
select u.user_id, user_id.user_rating, u.user_department
from (select (case when n.n in (1, 2, 3) or ud.numusers <= 5
then cast(substring_index(substring_index(users, ',', n.n), ',', -1) as unsigned)
else CAST(substring_index(substring_index(users, ',', 4 + rand()*(num_users - 3)), ',', -1) as unsigned)
end) as user_id
from (select user_department,
group_concat(user_id order by user_rating desc) as users,
count(*) as numusers
from t
group by user_department
) ud join
(select 1 as n union all select 2 union all select 3 union all select 4 union all select 5
) n
on n.n <= ud.numusers
) u join
t
on u.user_id = t.user_id
order by user_department, user_rating desc
关于mysql - 在 MySQL 查询中按记录排序并随机混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17660713/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
如何在Ruby中按名称传递函数?(我使用Ruby才几个小时,所以我还在想办法。)nums=[1,2,3,4]#Thisworks,butismoreverbosethanI'dlikenums.eachdo|i|putsiend#InJS,Icouldjustdosomethinglike:#nums.forEach(console.log)#InF#,itwouldbesomethinglike:#List.iternums(printf"%A")#InRuby,IwishIcoulddosomethinglike:nums.eachputs在Ruby中能不能做到类似的简洁?我可以只
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试将以下SQL查询转换为ActiveRecord,它正在融化我的大脑。deletefromtablewhereid有什么想法吗?我想做的是限制表中的行数。所以,我想删除少于最近10个条目的所有内容。编辑:通过结合以下几个答案找到了解决方案。Temperature.where('id这给我留下了最新的10个条目。 最佳答案 从您的SQL来看,您似乎想要从表中删除前10条记录。我相信到目前为止的大多数答案都会如此。这里有两个额外的选择:基于MurifoX的版本:Table.where(:id=>Table.order(:id).
我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时