select
delegation_cycle.delegation_cycle_id as task_id,
delegation_cycle.delegate_from_id as from_id,
delegation_cycle.delegate_to_id as to_id,
delegation_cycle.time_estimate as main_time_estimate,
delegation_cycle.status as main_task_status,
delegation_cycle.start_date as main_start_date,
delegation_cycle.changes,
DATE(delegation_cycle.delegated_time) as orginal_email_time,
tasks.task_name,
tasks.client_id,
tasks.followup,
tasks.task_owner,
tasks.main_due_date,
tasks.main_priority,
1 as delegated,
delegation_user_relation.showinlist,
users.email as email_from,
clients.client_name,
(select
count(delegation_cycle_id)
from
delegation_cycle
where
delegation_cycle.task_id = tasks.task_id
and delegation_cycle.task_parent = '2') as delegationcount,
(select
sum(status)
from
delegation_cycle
where
delegation_cycle.task_id = tasks.task_id
and delegation_cycle.task_parent = '2'
and delegation_cycle.status = '4') as sumstatus,
(select
count(notes_records_id)
from
notes_records
join
notes ON notes.notes_id = notes_records.notes_id
where
notes_to = '2'
and notes.task_id = delegation_cycle.task_id) as notescount,
(select
count(notes_records_id)
from
notes_records
join
notes ON notes.notes_id = notes_records.notes_id
where
notes_to = '2'
and notes.task_id = delegation_cycle.task_id
and notes_records.read = 0) as unreadnotescount
from
delegation_cycle
inner join
tasks ON tasks.task_id = delegation_cycle.task_id
inner join
users ON users.user_id = delegation_cycle.delegate_from_id
inner join
clients ON clients.client_id = tasks.client_id
join
delegation_user_relation ON delegation_user_relation.delegate_task_id = tasks.task_id
where
delegation_cycle.delegate_to_id = '2'
and delegation_cycle.status != 4
and case
when
delegation_cycle.status != 3
then
(delegation_user_relation.showinlist = 1
|| delegation_user_relation.showinlist = 2)
and delegation_user_relation.user_id = '2'
else delegation_user_relation.user_id = '2'
end
你好这个查询正在运行,我想检查使用delegationcount
和sumstatus
在哪里条件与case语句这样
when delegation_cycle.status != 3 && delegationcount*4!=sumstatus then
(delegation_user_relation.showinlist = 1 || delegation_user_relation.showinlist = 2)
and delegation_user_relation.user_id = '2' else delegation_user_relation.user_id = '2'
end
我想在where条件中使用alise名称,但是对于我这个查询不可能,所以我想重新构建此查询以在where where条件中使用alise名称
答案 0 :(得分:1)
您可以在查询中定义临时会话变量,并在where子句中使用相同的内容。
select
delegation_cycle.delegation_cycle_id as task_id,
delegation_cycle.delegate_from_id as from_id,
delegation_cycle.delegate_to_id as to_id,
delegation_cycle.time_estimate as main_time_estimate,
delegation_cycle.status as main_task_status,
delegation_cycle.start_date as main_start_date,
delegation_cycle.changes,
DATE(delegation_cycle.delegated_time) as orginal_email_time,
tasks.task_name,
tasks.client_id,
tasks.followup,
tasks.task_owner,
tasks.main_due_date,
tasks.main_priority,
1 as delegated,
delegation_user_relation.showinlist,
users.email as email_from,
clients.client_name,
@delegationcount:=(select
count(delegation_cycle_id)
from
delegation_cycle
where
delegation_cycle.task_id = tasks.task_id
and delegation_cycle.task_parent = '2') as delegationcount,
@sumstatus:=(select
sum(status)
from
delegation_cycle
where
delegation_cycle.task_id = tasks.task_id
and delegation_cycle.task_parent = '2'
and delegation_cycle.status = '4') as sumstatus,
@notescount:=(select
count(notes_records_id)
from
notes_records
join
notes ON notes.notes_id = notes_records.notes_id
where
notes_to = '2'
and notes.task_id = delegation_cycle.task_id) as notescount,
@unreadnotescount:=(select
count(notes_records_id)
from
notes_records
join
notes ON notes.notes_id = notes_records.notes_id
where
notes_to = '2'
and notes.task_id = delegation_cycle.task_id
and notes_records.read = 0) as unreadnotescount
from
delegation_cycle
inner join
tasks ON tasks.task_id = delegation_cycle.task_id
inner join
users ON users.user_id = delegation_cycle.delegate_from_id
inner join
clients ON clients.client_id = tasks.client_id
join
delegation_user_relation ON delegation_user_relation.delegate_task_id = tasks.task_id
where
delegation_cycle.delegate_to_id = '2'
and delegation_cycle.status != 4
and case
when
delegation_cycle.status != 3
then
(delegation_user_relation.showinlist = 1
|| delegation_user_relation.showinlist = 2)
and delegation_user_relation.user_id = '2'
else delegation_user_relation.user_id = '2'
end
并更改新的when ...
条款,如下所示:
when
delegation_cycle.status != 3 &&
@delegationcount*4!=@sumstatus then
(delegation_user_relation.showinlist = 1 ||
delegation_user_relation.showinlist = 2
) and delegation_user_relation.user_id = '2'
else
delegation_user_relation.user_id = '2'
end