按多列对数据库中的现有用户进行排序
Sorting existing users in the database by multiple columns
我使用了排序功能
sub sort_users {
my $self = shift;
return $self->search(undef, { order_by => [{ -desc => 'data_begin' },
{ -asc => 'username' }]});
}
根据开始日期过滤用户,如果相等则按用户名排序,数据库如下:
user1 2019-09-26 00:00:00
user2 2019-09-26 00:00:00
user3 2019-09-26 00:00:00
user4 2019-09-26 00:00:00
以下排序有时会以以下方式显示用户
user2, user3, user1, user4
其他时间
user1, user2, user3, user4.
编辑
我 运行 和 DBIC_TRACE = 1
我得到了:
SELECT COUNT( * ) FROM user_access_rights me WHERE ( ( access_rights_id = ? AND date_end >= ? ) ): 'level1', '2019-10-28 07:43:30+0000'
SELECT access_rights_recipient.username, access_rights_recipient.last_login, me.access_rights_id FROM user_access_rights me JOIN users access_rights_recipient ON access_rights_recipient.username = me.username WHERE ( ( access_rights_id = ? AND date_end >= ? ) ) ORDER BY date_begin DESC, username ASC LIMIT ?: 'level1', '2019-10-28 07:43:30+0000', '2'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user1'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user2'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user3'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user4'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user5'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user6'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user7'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user8'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user9'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user10'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user11'
需要显示user1和user2,而不是显示user5和user1
这是因为与具有级别 1 的用户相比,user5 在数据库中的添加时间晚了一秒钟,因此他们按该顺序显示,因为他们按 data_begin 按功能降序排列.
我使用了排序功能
sub sort_users {
my $self = shift;
return $self->search(undef, { order_by => [{ -desc => 'data_begin' },
{ -asc => 'username' }]});
}
根据开始日期过滤用户,如果相等则按用户名排序,数据库如下:
user1 2019-09-26 00:00:00
user2 2019-09-26 00:00:00
user3 2019-09-26 00:00:00
user4 2019-09-26 00:00:00
以下排序有时会以以下方式显示用户
user2, user3, user1, user4
其他时间
user1, user2, user3, user4.
编辑
我 运行 和 DBIC_TRACE = 1
我得到了:
SELECT COUNT( * ) FROM user_access_rights me WHERE ( ( access_rights_id = ? AND date_end >= ? ) ): 'level1', '2019-10-28 07:43:30+0000'
SELECT access_rights_recipient.username, access_rights_recipient.last_login, me.access_rights_id FROM user_access_rights me JOIN users access_rights_recipient ON access_rights_recipient.username = me.username WHERE ( ( access_rights_id = ? AND date_end >= ? ) ) ORDER BY date_begin DESC, username ASC LIMIT ?: 'level1', '2019-10-28 07:43:30+0000', '2'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user1'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user2'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user3'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:29', '2019-10-29T07:43:29', 'user4'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level1', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user5'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user6'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user7'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user8'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user9'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user10'
INSERT INTO user_access_rights ( access_rights_id, date_begin, date_end, username) VALUES ( ?, ?, ?, ? ) RETURNING id: 'level2', '2019-10-28T07:43:30', '2019-10-29T07:43:30', 'user11'
需要显示user1和user2,而不是显示user5和user1
这是因为与具有级别 1 的用户相比,user5 在数据库中的添加时间晚了一秒钟,因此他们按该顺序显示,因为他们按 data_begin 按功能降序排列.