mysql - 在 sqlite 中如何完成“如果某列存在则 select,否则 insert”这样的原子操作?
大家讲道理
大家讲道理 2017-04-17 11:34:25
0
1
594

只知道 replace into 的功能,不知道怎样 select。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

모든 응답(1)
黄舟

INSERT OR REPLACE INTO TABLE name (f1, f2, f3) VALUES (v1, v2, v3)

嗯,看错题了,我还以为是insert or update,结果是select or insert。

select or insert除了开transaction之外,我一时也没想出什么好办法,不过倒是有个select with default value的方案

select id, f1, f2 from sometable where id=3 union
select 999 as id, 'default_value1' as f1, 'default_value2' as f2
where not exists (select 1 from sometable where id=3)

999是id的default value

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!