有条件地插入并返回rowid还是0?
我知道如何使用交易来执行此操作,但我想知道我是否可以在一行中进行此操作。我的实际查询更为复杂,但我不知道的部分是如何获取rowID或0,而无需重复wery子句
insert into comment
(select @text, @userid, @date)
where (select count(*) from comment where body=@text and userid=@userid) == 0
select last_insert_rowid()
I know how to do this using a transaction but I was wondering if I can do this in a single line. My actual query is more complex but the part I can't figure out is how to get the rowid or 0 without repeating the where clause
insert into comment
(select @text, @userid, @date)
where (select count(*) from comment where body=@text and userid=@userid) == 0
select last_insert_rowid()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的SQLite版本为3.35.0+,则可以使用 a> clause to get the
rowid
of the inserted row like this:The drawback is that in case of a failed插入查询什么都没有返回。
If your app can check the number of returned rows you can translate that as 0.
See the demo< /a>。
If your version of SQLite is 3.35.0+ you can use the
RETURNING
clause to get therowid
of the inserted row like this:The drawback is that in case of a failed insertion the query returns nothing.
If your app can check the number of returned rows you can translate that as 0.
See the demo.
什么都不做。如果没有更新,您的查询已经返回0。
last_insert_rowid document
和
Do nothing. Your query already returns 0 if there was no update.
The last_insert_rowid documentation says
And the documentation for sqlite3_last_insert_rowid says