MYSQL 从参数值中选择多个值的字段

MYSQL Choosing field's multiples values from a parameter's value

(第一条消息!)。

我遇到了一个不知道如何解决的问题:

我知道了 select

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”,还有一个 table 的字段 (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

如果有人告诉我如何正确操作,我将不胜感激。

如果没有 table 定义就有点难做,这样我们就可以测试了。但是这样的事情应该有效:

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