Mysql case语句中的多个条件
我的表格中有以下字段 id、用户id、发货类型、库存类型、发票编号 库存类型为枚举(本地/国际) 我想得到每行的发票号。如果inv_type='local',则我希望invoice_no将'local_inv_no'作为别名,如果是international,则将'int_inv_no'作为别名。 如果其中一个为Null,则我希望结果中的“local\u inv\u no”为Null或int\u inv\u no为Null 下面是我的最新尝试Mysql case语句中的多个条件,mysql,Mysql,我的表格中有以下字段 id、用户id、发货类型、库存类型、发票编号 库存类型为枚举(本地/国际) 我想得到每行的发票号。如果inv_type='local',则我希望invoice_no将'local_inv_no'作为别名,如果是international,则将'int_inv_no'作为别名。 如果其中一个为Null,则我希望结果中的“local\u inv\u no”为Null或int\u inv\u no为Null 下面是我的最新尝试 $sql = "select case when
$sql = "select case when b.inv_type='local'
then (case when b.invoice_no is NULL then 'n' else b.invoice_no end 'local_inv')
else
when n.inv_type='int' then (case when b.invoice_no is NULL then 'n' else b.invoice_no end 'int_inv')
end 'inv_status'
from user_invoice b
where b.user_id = '$active_id'
";
但它似乎没有给我结果。我理解你的问题,就像你想要两列一样,对吗?当时,不要将其放在一个
案例中,因为这会导致一列
select
if(b.inv_type='local', coalesce(b.invoice_no, 'n'), NULL) as local_inv_no,
if(b.inv_type='international', coalesce(b.invoice_no, 'n'), NULL) as int_inv_no
from user_invoice b
where b.user_id = '$active_id'
coalesce()
函数是构建时的案例的缩写形式。它只返回它的第一个参数,即非null
if()
也比case when
要少写一点,因为它的参数是if(,)
我理解你的问题就像你想要两列一样,对吗?当
时,不要将其放在一个案例中,因为这会导致一列
select
if(b.inv_type='local', coalesce(b.invoice_no, 'n'), NULL) as local_inv_no,
if(b.inv_type='international', coalesce(b.invoice_no, 'n'), NULL) as int_inv_no
from user_invoice b
where b.user_id = '$active_id'
coalesce()
函数是构建时的案例的缩写形式。它只返回它的第一个参数,即非null
if()
也比case when
要少写一点,因为它的参数是if(,)
看起来很像混合语句
既然我不知道你想说什么,我就尽力重建一份工作报表
select
case when b.inv_type='local'
then (
case when b.invoice_no is NULL
then
'n'
else
b.invoice_no
end)
else
(
case when n.inv_type='int'
then
(
case when b.invoice_no is NULL
then
'n'
else
b.invoice_no
end
)
end
)
end
from user_invoice b
where b.user_id = '$active_id'
我不明白为什么你的声明中有“投资状态”和“国际投资”。
但这将是一个完全可以工作的SQL语句,正如用Mimer SQL检查的一样
既然我不知道你想说什么,我就尽力重建一份工作报表
select
case when b.inv_type='local'
then (
case when b.invoice_no is NULL
then
'n'
else
b.invoice_no
end)
else
(
case when n.inv_type='int'
then
(
case when b.invoice_no is NULL
then
'n'
else
b.invoice_no
end
)
end
)
end
from user_invoice b
where b.user_id = '$active_id'
我不明白为什么你的声明中有“投资状态”和“国际投资”。
但这将是一个完全工作的SQL语句,正如用Mimer SQL检查的那样,考虑提供DDL和/或SQLFIDLE,以及所需的结果。“不给结果”?此查询将生成一条错误消息!考虑提供DDLs和/或SqLFIDLE,连同期望的结果。“不给结果”?此查询将生成一条错误消息!