admin管理员组文章数量:1317906
Having a pretty simple dataset in SQLite3. I wish INSERT
to be executed only-if last row's data is different.
I want to avoid TRIGGER
: I need to let my application decide whether the above filtering is required, or not (in case not, there is a simple insertion).
Here is the SQLFiddle demo.
Table definition:
CREATE TABLE Test (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date datetime,
text TEXT,
text2 TEXT
);
Sometimes I need to insert data only-if last insertion is different (text field is different than our to-be-inserted text).
So I need to use some basic sub-query which gets me permission in case last row is not the same.
Can I do this subquery within one step (i.e. not more subqueries):
Something like this, but this is having syntax-error.
SELECT 1 FROM Test ORDER BY id DESC LIMIT 1 HAVING text="mynewtext"
Maybe with WINDOW functions? Or simple, single subquery:
INSERT INTO Test
SELECT null, '2025-01-22 10:10:03', 'Data5', 'DATA2'
WHERE (
SELECT (text!="Data5")+(text2!="DATA2") from Test ORDER BY id DESC LIMIT 1);
This above solution works. But I keep it here as reference, some may have idea to improve.
Having a pretty simple dataset in SQLite3. I wish INSERT
to be executed only-if last row's data is different.
I want to avoid TRIGGER
: I need to let my application decide whether the above filtering is required, or not (in case not, there is a simple insertion).
Here is the SQLFiddle demo.
Table definition:
CREATE TABLE Test (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date datetime,
text TEXT,
text2 TEXT
);
Sometimes I need to insert data only-if last insertion is different (text field is different than our to-be-inserted text).
So I need to use some basic sub-query which gets me permission in case last row is not the same.
Can I do this subquery within one step (i.e. not more subqueries):
Something like this, but this is having syntax-error.
SELECT 1 FROM Test ORDER BY id DESC LIMIT 1 HAVING text="mynewtext"
Maybe with WINDOW functions? Or simple, single subquery:
INSERT INTO Test
SELECT null, '2025-01-22 10:10:03', 'Data5', 'DATA2'
WHERE (
SELECT (text!="Data5")+(text2!="DATA2") from Test ORDER BY id DESC LIMIT 1);
This above solution works. But I keep it here as reference, some may have idea to improve.
Share Improve this question edited Jan 22 at 18:23 Daniel asked Jan 22 at 17:06 DanielDaniel 2,6024 gold badges32 silver badges75 bronze badges 2 |1 Answer
Reset to default 0Something like this:
INSERT INTO Test(date, text, text2)
SELECT n.*
FROM (
VALUES('2025-01-22 10:10:03', 'Data5', 'DATA2')
) AS n
WHERE (n.column2, n.column3) NOT IN (
SELECT text, text2
FROM Test
ORDER BY id DESC
LIMIT 1
);
本文标签: sqliteInsert only if last row differsperformance considerationsStack Overflow
版权声明:本文标题:sqlite - Insert only if last row differs - performance considerations - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742032032a2416626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
text
can be UNIQUE, you can useREPLACE
sqlitetutorial/sqlite-replace-statement – yotheguitou Commented Jan 23 at 13:22text
nortext2
can be unique. – Daniel Commented Jan 23 at 17:02