SQL更新和插入
我想
UPDATE CATALOGUE
SET CATALOGUE.NUMBERINSTOCK=NUMBERINSTOCK-1
WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID
FROM ALBUM
WHERE ALBUM.TITLE = '20 People in a Field'
AND IS_DISTRIBUTED_AS ='c')
这样
INSERT INTO ORDERELEMENT VALUES (1,10,12,17)
如果这是真的,
(SELECT NUMBERINSTOCK
FROM CATALOGUE
WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID
FROM ALBUM
WHERE ALBUM.TITLE = '20 People in a Field'
AND IS_DISTRIBUTED_AS ='c')) > 0
做。我遇到的问题是找到如何在 SQL 查询中执行条件语句,以及如何作为一个查询的一部分进行更新和插入。
我必须将所有这些事情作为一个查询来完成。
I want to do this
UPDATE CATALOGUE
SET CATALOGUE.NUMBERINSTOCK=NUMBERINSTOCK-1
WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID
FROM ALBUM
WHERE ALBUM.TITLE = '20 People in a Field'
AND IS_DISTRIBUTED_AS ='c')
and this
INSERT INTO ORDERELEMENT VALUES (1,10,12,17)
if this is true
(SELECT NUMBERINSTOCK
FROM CATALOGUE
WHERE CATALOGUE.ITEMID =(SELECT ALBUM.ALBUM_ID
FROM ALBUM
WHERE ALBUM.TITLE = '20 People in a Field'
AND IS_DISTRIBUTED_AS ='c')) > 0
The problem I have is finding how to do a conditional statement in an SQL query, and also how to do an update and insert as part of one query.
I must do all these things as one query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在一条语句中更新一张表并插入另一张表。
如果指定 WHERE 子句,则可以有条件地执行 UPDATE。您可以使用 insert 完成同样的事情(详细信息取决于 dbms)。
不过,您可以创建一个包含条件逻辑的存储过程。该过程的调用可能算作“一个”语句吗?
You can't update one table and insert into another table in one statement.
It is possible to conditionally perform an UPDATE if you specify a WHERE clause. You can acomplish the same thing with insert (details depends on dbms).
You can however create a stored procedure that contains your conditional logic. The invokation of that procedure might count as "one" statement?