在case语句sql中选择语句

时间:2014-10-10 16:22:25

标签: mysql sql database

如何在语句之间执行sql语句?

我正在尝试执行以下查询并收到错误:

select *, 
case when 'trans_type' <> 'Stock' then 
lname else (select item_name from trans_i where id = a.id limit 0,1) end as lname
from 'transaction' as a

数据库是MySql

错误:

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select item_name from trans_i limit 0,1

ps:我使用交易作为表名仅用于演示目的,它不是&#39;交易&#39;在我的真实数据库中。

1 个答案:

答案 0 :(得分:1)

我认为这应该是正确的陈述。

select *, 
(case when trans_type <> 'Stock' then 
a.lname else (select item_name from trans_i ti where ti.id = a.id limit 0, 1)
end) as 'lname' from transaction as `a`