android - Finding all the entries in a table having the column containing a substring
问题描述
I am using Room database in android(SQL) and I have a table that contains date as a column stored as a string of format 2000-01-31. I want to get all the entries in the table according to their respective month. I was thinking of using
SELECT * FROM `Transactions` WHERE Date LIKE '%01%
But in this way, I will have to write separate queries for each month. I want to get the entries month-wise and show them in my android application.
解决方案
这回答了最初提出的问题。
不要对日期使用字符串操作。如果您想要特定年份的特定月份,请使用:
where date >= '2021-01-01' and date < '2021-02-01'
如果你想要所有的一月,那么像这样:
where month(date) = 1
month()
是用于此目的的常用函数,但它可能因您的数据库而异。
推荐阅读
- java - 返回生成的键时,MySQL JDBC 驱动程序错误地将 bigint 报告为 BigInteger
- java - java jooq合并2个表
- node.js - 内部/模块/cjs/loader.js Heroku
- git - 部署到master时如何防止travis构建循环
- python - 两个子类的继承方法中的参数数量不等
- javascript - 查找并替换导致重复的单词
- python - 如何在某些条件下将字典中的值附加到字典列表中
- python - Flask 部署到 heroku 错误
- java - 使用 removeValue() 删除 firebase 子时,应用程序因 IndexOutOfBoundsException 而崩溃
- google-sheets - 使用 countif 或任何函数在谷歌表中求和值