我在表帐户中有一列已定义为double。我想更新我正在呼叫的account_balance
字段。
这是我的代码
Query theQuery2 = currentSession.createSQLQuery("update account set account_balance =
(select account_balance from account where telephone_number = ?4 AND nid_number = ?5) + ?6 where telephone_number = ?4 AND nid_number = ?5") ;
theQuery2.setParameter(6, the_loss);
theQuery2.setParameter(5, nid_number);
theQuery2.setParameter(4, telephone_number);
theQuery2.executeUpdate();
这是mysql查询
update account
set account_balance = (select account_balance where telephone_number = ?4 AND nid_number = ?5) + ?6
我如何进行查询
(select account_balance where telephone_number = ?4 AND nid_number = ?5)
给我一个double
类型的值,以便我添加准备好的值?6,它是java中double类型的值。
答案 0 :(得分:0)
此查询:
update account
set account_balance = (select account_balance where telephone_number = ?4 AND nid_number = ?5) + ?6
真的没有道理。想必您打算:
update account
set account_balance = account_balance + ?6
where telephone_number = ?4 and nid_number = ?5;
换句话说,过滤器在update
上进行,而嵌套的select
是不必要的。
您真的不需要担心类型。您正在传递值作为参数,并且如果参数是数字类型(即支持加法),那么它将被添加到account_balance
中。
我要说的是,货币金额应使用numeric
/ decimal
(即固定点值)而不是两倍(浮点值)来存储。