【问题标题】:select statement in case statement sqlcase语句sql中的select语句
【发布时间】:2014-12-05 21:13:42
【问题描述】:

如何在 case when 语句之间执行 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:我使用事务作为表名仅用于演示目的,它在我的真实数据库中不是“事务”。

【问题讨论】:

  • 你遇到了什么错误?
  • transaction 是保留字。尝试用反引号括起来:...FROM `transaction` .
  • @RocketHazmat :我仅将交易用于演示目的。它不是我的真实表名。

标签: mysql sql database


【解决方案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`

【讨论】:

    猜你喜欢
    • 2015-02-27
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 2012-05-18
    相关资源
    最近更新 更多