我想获取按日期排序的最近一年的数据记录。但问题是我需要从过去一年的第一天开始的数据。
例如,如果今天是15-June-2016
,那么我需要从1-June-2015
获取数据。
我试着用
做SELECT *
FROM Table
WHERE
date >= DATE_SUB(NOW(),INTERVAL 1 YEAR)
ORDER BY date
但它没有用。
答案 0 :(得分:2)
试试这个
SELECT *
FROM Table
WHERE
date >= DATEADD(YEAR,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
ORDER BY date
答案 1 :(得分:1)
要获得上一年的第一个月,请使用:
public void showPermissionsDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int hasWriteExternalStoragePermission = activity.checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
int hasWriteGetAccountsPermission = activity.checkSelfPermission(android.Manifest.permission.GET_ACCOUNTS);
if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED || hasWriteGetAccountsPermission != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.GET_ACCOUNTS}, MainActivity.REQUEST_CODE_ASK_PERMISSIONS);
} else {
isAllPermissionsGranted = true;
}
} else {
isAllPermissionsGranted = true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CODE_ASK_PERMISSIONS) {
if (grantResults.length > 0) {
boolean isAllPermissionsGranted = true;
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
isAllPermissionsGranted = false;
}
}
if (isAllPermissionsGranted) {
this.isAllPermissionsGranted = true;
} else {
android.widget.Toast.makeText(this, "Please, grand permissions", android.widget.Toast.LENGTH_LONG).show();
showPermissionsDialog();
}
}
}
}
您的查询应该是:
DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101'))
参考:
答案 2 :(得分:1)
SELECT *
FROM TABLE
WHERE
date > DATEADD(YEAR, -1, DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101'))
ORDER BY date
答案 3 :(得分:0)
在条件适用的地方使用。
从表中选择*,其中日期&gt; dateadd(年,-1,getdate()) - 日(getdate())+按日期排序
由于