随手记-记录oc中FMDB常用的SQL语句

// 创建表

create table ScoreTable (idx INTEGER PRIMARY KEY AUTOINCREMENT, userid INTEGER, skuid INTEGER)

// 插入数据

insert into ScoreTable (userid, skuid) values (%ld,%ld)

// 删除数据

delete from ScoreTable where userid = %ld and skuid = %ld

// 修改数据

update ScoreTable set mission_use_time = %lld  where userid = %ld and skuid = %ld

// 查询相应id的数据

select * from words where id = %d

// 随机取三条 words 表中的 word,其中 id 不能为%ld, word 不能为空,不等于null

select word from words where id != %ld and word !='' and word is not null order by random() limit 3

// 查询 word_prop 表中的 wordId 不在 words 表 id 中的数据

select * from word_prop where wordId not in(select id from words)

// 查询word_prop中的数据个数

select count(*) from word_prop

// 查询 word_prop 表中 wordId 重复的数据

select * from word_prop group by wordId having count(*) > 1

// 查询 wordId 为8354的数据

select * from word_prop where wordId = 8354

// 更新 userid 列的数据都置为11

update ScoreTable set userid = 11

// 查询两个表相差的数据(a为数据多的表,b为数据少的表)

select * from a LEFT JOIN b ON b.word_id = a.id WHERE b.word_id is null

// 查询一个表中synchronize为0的数据 (至少10条数据)

select * from WordStateTable where userid = '3410209' and skuid = '4' and synchronize = 0 LIMIT 10

// 查询一个表中spokenid 为9999的插入最早的数据

select * from SpokenTestsTable where spokenid = '99999' order by idx limit 0,1

// 根据userId、skuid、spokenId获取今天新增的个数,如果在今天之前已经插入过的spokenId则不算(去重),createTime/1000---时间戳使用10位的

select count(DISTINCT t.spokenId) from SpokenTestsTable t where t.userId = %d and t.skuid = %ld and date(datetime(t.createTime/1000, 'unixepoch','localtime')) = date('now') and (select count(1) as num from SpokenTestsTable st where st.userId = t.userId and st.skuid = t.skuid and st.spokenId = t.spokenId and date(datetime(st.createTime/1000,'unixepoch','localtime')) < date('now')) = 0

你可能感兴趣的:(随手记-记录oc中FMDB常用的SQL语句)