我的应用程序有 2 个实体(或表)。
Category (id, name)
Item (id, name, amount, date, categoryId)
我需要的是
SELECT categoryId, Category.name,
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 1, amount, 0)) AS jan
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 2, amount, 0)) AS feb
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 3, amount, 0)) AS mar
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 4, amount, 0)) AS apr
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 5, amount, 0)) AS may
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 6, amount, 0)) AS jun
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 7, amount, 0)) AS jul
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 8, amount, 0)) AS aug
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 9, amount, 0)) AS sep
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 10, amount, 0)) AS oct
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 11, amount, 0)) AS nov
SUM(IF(MONTH(FROM_UNIXTIME(date)) = 12, amount, 0)) AS dec
FROM Item INNER JOIN Category ON Item.categoryId = Category.id
WHERE YEAR(FROM_UNIXTIME(date)) = 2012
GROUP BY categoryId
注意:上面的查询是用 MySQL 的方式编写的,我对 CoreData 和 Sqlite 完全陌生
这种情况下,CoreData VS Sqlite,哪个更适合?
如何将上面的sql翻译成CoreData或Sqlite形式?
谢谢。
编辑
我可以混合两者吗?For CRUD
is using CoreData,但是对于这样一个复杂的查询使用Sqlite (FMDB library)?
默认情况下,CoreData 创建了一个扩展名为.sqlite的文件。但是,在FMDB github 指令中,这里的扩展名为.db。有关系吗?
数据库路径[FMDatabase databaseWithPath:@"/tmp/tmp.db"];
和CoreData一样吗?