所以我想从系统中清除一些早于2年的数据。
我有一个我存储的日期时间字段。
我知道我可以做类似
的事情SELECT * FROM `table` where date_field < (??? what goes here)
我的问题是我想如何计算日期,或者我感到困惑
答案 0 :(得分:4)
使用DATE_SUB
.../api/examples/search/searchByName?filed1=value1&field3=value3
答案 1 :(得分:4)
对于手册不够清晰的具体答案:
SELECT * FROM `table` where date_field < (now() - interval 2 year)
答案 2 :(得分:3)
这可能会有所帮助:
SELECT EXTRACT(YEAR FROM `date`) AS year
from table
having year< (EXTRACT(YEAR from CURDATE())-2)
答案 3 :(得分:1)
您可以使用@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER:
String name = null;
int phoneIndex = -1;
int emailIndex = -1;
String phone = null;
String email = null;
try{
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
if(cursor.moveToFirst())
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = contactUri.getLastPathSegment();//the id you picked
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (cursorPhone != null && cursorPhone.moveToFirst()) {
phoneIndex = cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
Cursor cursorEmail = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if (cursorPhone != null && cursorEmail.moveToFirst()) {
emailIndex = cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
if(phoneIndex != -1){
phone = cursorPhone.getString(phoneIndex);
}
if(emailIndex != -1){
email = cursorEmail.getString(emailIndex);
}
Log.d(TAG, "name: " + name + " ,phone: " + phone + " ,email: " + email);
} catch (Exception e){
Log.e(TAG, e.getMessage());
}
}}
}
:
INTERVAL
引自https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html。
答案 4 :(得分:1)
SELECT * FROM table
其中date_field&lt; DATE_SUB(NOW(),INTERVAL 2年);