如何使用子查询返回的值插入或更新行?
编辑:向 J. Leffler、Cheese Con Queso 或任何熟悉 Informix-SE 的人寻求帮助。
Informix-SE 4.11:
我有一个名为“cuadre”的表,用于在每个工作日(周一至周六)结束时核对现金抽屉。一旦抽屉核对完毕,现金抽屉中的金额将存储在“cu_sa_cash”列中,该行被保存,系统关闭,商店当天关门,我们都去喝点啤酒。在工作日,我们启动系统,运行一个 SQL 过程,在 cuadre 表中为当天的过账创建(插入)一个新行,然后再次重复该循环。
我希望我的 SQL 过程插入当天的新行,其抽屉现金金额与前一个工作日行中存储的现金金额相同。请记住,这不是 SPL,因为我仍在使用 SE 4.11.. 因此,下面的 SQL 语句是我想要完成的总体思路,但它们不起作用!.. 你能为我提供解决方案?...谢谢!
INSERT INTO cuadre(cu_date,cu_en_dincaja)
VALUES (TODAY,(SELECT cu_sa_cash
FROM cuadre
WHERE cu_date = (SELECT MAX(cu_date)
FROM cuadre)));
或者
INSERT INTO cuadre(cu_date)
VALUES(TODAY);
UPDATE cuadre
SET cu_en_dincaja = (SELECT cu_sa_cash
FROM cuadre
WHERE cu_date = TODAY - 1)
WHERE cu_date = TODAY;
EDIT: Looking for help from J. Leffler, Cheese Con Queso or anyone who knows Informix-SE well.
Informix-SE 4.11:
I have a table called 'cuadre' which is used to reconcile the cash drawer at the end of each business day (Monday thru Saturday). Once the drawer has been reconciled, the amount in the cash drawer is stored in column 'cu_sa_cash', the row is saved and the system is shutdowned, the store is closed for the day and we all go have some beers.. On the next business day, we start up the system, run an SQL procedure that creates (inserts) a new row in the cuadre table for that days postings and repeat the cycle again.
I would like for my SQL proc to INSERT the new row for the current day with the same cash in drawer amount that was stored in the previous business days row. Keep in mind that this is not SPL, as I'm still using SE 4.11.. So, the below SQL statements are the general idea of what I want to accomplish, but they don't work!.. Can you provide me with solution?.. Thanks!
INSERT INTO cuadre(cu_date,cu_en_dincaja)
VALUES (TODAY,(SELECT cu_sa_cash
FROM cuadre
WHERE cu_date = (SELECT MAX(cu_date)
FROM cuadre)));
or
INSERT INTO cuadre(cu_date)
VALUES(TODAY);
UPDATE cuadre
SET cu_en_dincaja = (SELECT cu_sa_cash
FROM cuadre
WHERE cu_date = TODAY - 1)
WHERE cu_date = TODAY;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 informix 一无所知,但这是一种替代语法...
注意:如果 cu_date 不唯一,这可能会返回多行。
编辑 添加了 MySQL 解决方案,用于从读取和写入同一个表。值得在 Informix 中进行测试吗?
I know nothing about informix in particular, but this is an alternative syntax...
NOTE: This may return multiple rows if
cu_date
is not unique.EDIT Added MySQL work around for reading from and writing to the same table. Worth a test in Informix?
使用临时表是问题的解决方案
using a temp table was the solution to the problem