MYSQL选择字段的值是参数值的倍数
第一条消息 我遇到了一个我不知道如何解决的问题: 我选了这个MYSQL选择字段的值是参数值的倍数,mysql,parameters,case,selection,Mysql,Parameters,Case,Selection,第一条消息 我遇到了一个我不知道如何解决的问题: 我选了这个 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.c
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
如果有人告诉我如何正确操作,我将不胜感激。没有表定义,我们很难进行测试。但像这样的方法应该有效:
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
哦,我的上帝,这是如此可笑的简单;非常感谢!