【问题标题】:Get status based on other table column value mysql根据其他表列值mysql获取状态
【发布时间】:2015-07-08 01:40:14
【问题描述】:

我有两张桌子。 工作表。

job_id  job_type    job_type       User         job_time               job_name
1       1           Day            User1        2015-04-23 23:20:10   Job1
2       2           Night          User2        2015-04-23 13:20:10   Job2
3       3           Day & Night.   User3        2015-04-22 23:10:10   Job3
4       3           Day & Night.   User3        2015-04-20 13:40:10   Job4
5       1           Day            User4        2015-04-26 03:20:10   Job5

和 task_entry 表。

task_entry_id    job_id      task_option_type     emp_id        completed_task_count    status
1                1             Day                  101         10                      1 
2                1             Day                  102         0                       0           
3                1             Day                  103         5                       1 
4                2             Night                101         20                      1 
5                3             Day                  101         4                       1 
5                3             Night                101         0                       0 
6                3             Day                  102         10                      1 
7                3             Night                102         5                       1 

如果 job_table 的 job_type 为 3,那么 task_entry 表中将有 2 个条目,一个代表白天,一个代表夜间。 否则只有一个条目。

也就是说,如果 job_type 是 3,那么 count 应该是 count/2。 (早晚)。 其他计数应该是正常计数。

我想根据 job_id 获取状态。如果 job_id 的所有 task_entry 为 1,则状态应为“COMPLETE”,否则为“PENDING”
和完成的任务总数。

job_id      task_entry_count    status      completed_task_count
1           3                   PENDING     15
2           1                   COMPLETE    20
3           2                   PENDING     19

我怎样才能得到它。

【问题讨论】:

    标签: mysql status


    【解决方案1】:

    试试这个查询

    select job_id,count(task_entry_count),case when a.job_id is null then 'Compeletd' else 'pending'end as status,sum(completed_task_count)
    from task_table
    join (select job_id from task_table where status=0) a a.job_id =task_table.job_id
    group by job_id
    

    【讨论】:

    • 这是我的查询,用于获取 task_entry_count。 SELECT jt.job_id, ROUND(SUM (CASE WHEN jt.job_type = 3 THEN 0.5 ELSE 1 END), 0) AS task_entry_count FROM job_table jt INNER JOIN task_entry te ON jt.job_id = te.job_id GROUP BY jt.job_id
    猜你喜欢
    • 2016-12-22
    • 2021-09-27
    • 1970-01-01
    • 2022-07-06
    • 2018-09-24
    • 2016-11-30
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多