MYSQL 从参数值中选择字段的倍数值。

问题描述 投票:-1回答:1

(第一条信息!)。

我有个问题不知道怎么解决。

我得到了这个选择

select
p.id as pId, -- 0
p.code,
p.name 

from work_order as wo

left join project as p on wo.project_fk = p.id

where date(wo.date1) >= :initialDate
and date(wo.date1) <= :initialDate

group by p.id

order by p.code asc

我得到了一个参数(:state),有3个可能的值'1'、'2'或'3',以及一个有多个值的表域(state_fk)。我希望根据参数的值来选择state_fk的值。接下来的代码是错误的,但它可以给你和想法,我需要什么。

select
p.id as pId, -- 0
p.code,
p.name 

from work_order as wo

left join project as p on wo.project_fk = p.id

where date(wo.date1) >= :initialDate
and date(wo.date1) <= :initialDate
and ( (case :state = '1' then ('abc', 'qwe') end) in state_fk 
    or (case :state = '2' then ('cdw', 'zxc', 'ere') end) in state_fk
    or (case :state = '3' then ('swq', 'bge', 'qah') end) in state_fk )

group by p.id

order by p.code asc

如果有人能告诉我如何正确地做,我会非常感激。

mysql parameters case selection
1个回答
0
投票

如果没有表的定义,我们就很难做到这一点,这样我们就可以测试了。但是像这样的东西应该可以用。

select
p.id as pId, -- 0
p.code,
p.name 

from work_order as wo

left join project as p on wo.project_fk = p.id

where date(wo.date1) >= :initialDate
and date(wo.date1) <= :initialDate
and ( 
        (:state = '1' and state_fk in ('abc', 'qwe'))
        OR
        (:state = '2' and state_fk in ('cdw', 'zxc', 'ere'))
        OR
        (:state = '3' and state_fk in ('swq', 'bge', 'qah'))
    )

group by p.id

order by p.code asc
© www.soinside.com 2019 - 2024. All rights reserved.