【问题标题】:count(*) doesn't return 0count(*) 不返回 0
【发布时间】:2020-12-22 22:19:51
【问题描述】:

我有一个 sql 问题,请帮我这是我的查询

  select count(category_value .list_value_id) as jobs , category_type.value as category
from list_values category_type
full outer join params category_value
on category_type.list_value_id = category_value .list_value_id
join qrtz_triggers jobs
on category_value .object_id = jobs.job_name
and jobs.trigger_state ='PAUSED'
and category_value.attr_id = 9158075153713931109
where category_type.attr_type_def_id = 9158075154713931109
group by category_type.value;

attr_id 是包含 9158075158713931109(非关键)和 9158075157713931109(关键)的列表值

returns result:
JOBS,   CATEGORY
2        Non-Critical


expected result is :
JOBS,   CATEGORY
2        Non-Critical
0          Critical

list_values table conains
list_value_id          values
9158075158713931109     non-critical
9158075157713931109      critical


params table
list_value_id          attr_id                         object_id
9158075158713931109     9158075153713931109           a
9158075157713931109      9158075153713931109          b
9158075157713931109      9158075153713931109          c
9158075158713931109     9158075153713931109           d

qtz_trigger table  i need triggers with state "paused"

job_name            trigger_state 
b                     paused
a                     paused
e                     normal
c                     paused

我什至尝试了 category_value .object_id in (select jobs.job_name from qrtz_triggers jobs where jobs.trigger_type ='SIMPLE') 但得到错误命令未正确结束

【问题讨论】:

  • MySQL 不支持full outer join。您确定这是您使用的数据库吗?
  • 关于“命令未正确结束”语法错误:请删除所有空格:category_value.list_value_id(与“category_value .list_value_id:)”

标签: sql count left-join full-outer-join


【解决方案1】:

我认为你实际上想要left joins,从list_values 表开始,然后是paramsqrtz_triggers

select count(t.job_name) as jobs, v.value as category
from list_values v
left join params p
    on  p.list_value_id = v.list_value_id
    and p.attr_id       = v.attr_type_def_id
left join qrtz_triggers t
    on  t.job_name      = p.object_id
    and t.trigger_state = 'PAUSED'
where v.attr_type_def_id = 9158075154713931109
group by v.value;

原始查询中的条件有点混乱,所以我尝试重新安排 - 您可能需要查看它。我还使用了简短且具有代表性的表别名。

【讨论】:

    【解决方案2】:

    这个过滤条件:

    where category_type.attr_type_def_id = 9158075154713931109
    

    将把full join 变成左连接或右连接。

    full joins 过滤是相当棘手的。我不确定是否需要 full join - 你的问题并没有解释你真正想要做什么。但是,如果您确实需要full join,我发现子查询中的过滤通常可以满足我的需求。

    【讨论】:

    • 我需要一个查询来获取在关键(9158075157713931109)和非关键(9158075158713931109)触发器计数中状态为“暂停”的触发器
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-05
    • 2016-10-19
    • 1970-01-01
    相关资源
    最近更新 更多