简体   繁体   English

在单独的表中从键/值对中选择不同的值

[英]Selecting distinct values from key/value pairs in a seperate table

I have 2 database tables 我有2个数据库表

JOBS(JOB_ID, JOB_TIME, JOB_NAME,...), JOB_PARAMETERS(JOB_ID,NAME,VALUE)

where JOB_PARAMETERS is essentially a map containing job parameter key value pairs. 其中JOB_PARAMETERS本质上是一个包含作业参数键值对的映射。 Every job may have a unique parameter key/value pairs. 每个作业可能具有唯一的参数键/值对。

I am looking to pragmatically build a query that will return distinct job id's that contain key/value combinations. 我期待以实用的方式构建一个查询,该查询将返回包含键/值组合的不同作业ID。 Where the values are actually a list of values, comparison operators. 其中值实际上是值列表,比较运算符。 For example: 例如:

JOB_PARAMETERS: NAME = 'OUTPUT_FILENAME', VALUE LIKE "ALEX%", "JAX%"
                NAME = 'PRIORITY' , VALUE > 7

The above example would automatically filter out all jobs that don't have the OUTPUT_FILENAME and PRIORITY key. 上面的示例将自动过滤掉所有没有OUTPUT_FILENAME和PRIORITY键的作业。 Returning All jobs that meet both conditions. 返回满足这两个条件的所有工作。

I also need to be able to support pagination and order by. 我还需要能够支持分页和排序。

I was planning on using Perl with DBIx::Class, But I can do it in pure Perl/SQL as well. 我计划将Perl与DBIx :: Class一起使用,但我也可以在纯Perl / SQL中使用它。

I am open to changing the database schema, but every job can have different key/value pairs, so I cant just make them columns in the jobs table. 我愿意改变数据库模式,但每个作业都可以有不同的键/值对,所以我不能只在job表中创建它们。

Thanks in advance. 提前致谢。

One can do it in SQL, by grouping JOB_PARAMETERS by JOB_ID and filtering the groups accordingly. 可以在SQL中执行此操作,方法是将JOB_PARAMETERSJOB_ID分组并相应地过滤组。 For example, if there is a uniqueness constraint over (JOB_ID, NAME) , one can query as follows: 例如,如果(JOB_ID, NAME)存在唯一性约束,则可以按如下方式进行查询:

SELECT   JOB_ID
FROM     JOB_PARAMETERS
WHERE    (NAME='OUTPUT_FILENAME' AND (VALUE LIKE 'ALEX%' OR VALUE LIKE 'JAX%'))
      OR (NAME='PRIORITY' AND VALUE > 7)
GROUP BY JOB_ID
HAVING   COUNT(*) = 2

Absent such a uniqueness constraint, COUNT(*) would have to be replaced eg with COUNT(DISTINCT NAME) . 如果没有这样的唯一性约束,则必须用COUNT(DISTINCT NAME)替换COUNT(*) COUNT(DISTINCT NAME)

When using DBIx::Class you can generate a DBIC schema by using Schema::Loader. 使用DBIx :: Class时,可以使用Schema :: Loader生成DBIC模式。

After connecting to the database you get a $schema object you can use to get a ResultSet filtered to return the Result objects you want: 连接到数据库后,您将获得一个$ schema对象,您可以使用该对象来过滤ResultSet以返回所需的Result对象:

my $rs_job_parameters = $schema->resultset('Job_Parameters')->search({
    -or => [
        {
            'name' => 'OUTPUT_FILENAME',
            'value' => [{ like => 'ALEX%'}, { like => 'JAX%' }].
        },
        {
            'name' => 'PRIORITY',
            'value' => [{ '>' => 7}].
        }
    ]},
    {
        columns  => [qw( job_id )],
        group_by => [qw( job_id )], # alternative you can use distinct => 1 to group_by all selected columns
        having   => \[ 'COUNT(*) = ?', [ 2 ] ],
    }
);

my @job_ids = $rs_job_parameters->get_column('job_id')->all;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM