[英]DBIx::Class : Resultset order_by based upon existence of a value in the list
I am using DBIx::Class and I have got a ResultSet. 我正在使用DBIx :: Class,并且有一个ResultSet。 I like to re-order the ResultSet.
我想对ResultSet重新排序。 I like to check a particular column "City" against a fix list of values ("London", "New York" "Tokyo") If city is found in the list of values I like to move that result to the top group.
我喜欢根据固定的值列表(“伦敦”,“纽约”,“东京”)检查特定的“城市”列。如果在值列表中找到城市,我希望将该结果移至顶部组。 If city is not found, I like to move that result to the bottom group in the ResultSet.
如果未找到城市,我希望将该结果移至ResultSet中的底部组。
ORDER BY expr might be what you're looking for. ORDER BY expr可能就是您想要的。
For example, here a table: 例如,这里有一个表:
mysql> select * from test;
+----+-----------+
| id | name |
+----+-----------+
| 1 | London |
| 2 | Paris |
| 3 | Tokio |
| 4 | Rome |
| 5 | Amsterdam |
+----+-----------+
Here the special ordering: 这里是特殊订购:
mysql> select * from test order by name = 'London' desc,
name = 'Paris' desc,
name = 'Amsterdam' desc;
+----+-----------+
| id | name |
+----+-----------+
| 1 | London |
| 2 | Paris |
| 5 | Amsterdam |
| 3 | Tokio |
| 4 | Rome |
+----+-----------+
Translating this into a ResultSet method: 将其转换为ResultSet方法:
$schema->resultset('Test')->search(
{},
{order_by => {-desc => q[name in ('London', 'New York', 'Tokyo')] }}
);
Something like: 就像是:
#!/usr/bin/env perl
use strict;
use warnings;
my $what = shift or die;
my @ary = qw(alpha beta gamma);
unshift(@ary,$what) unless ( grep(/$what/,@ary) );
print "@ary\n";
1;
Run as: 运行方式:
./myscript omega
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.