如何在使用union all查询时在desc中排序日期。现在让我解释我的问题我已经使用union all查询从两个表中获取数据现在我需要在desc中订购数据,当我尝试执行时获取日期格式:第一个ORDER BY术语与任何列都不匹配结果集
让我发布我迄今为止尝试过的代码:
public List<VisitModel> visitLists(int id,String date){
String visitquery= " Select * from ( Select " + VisitModel.Visit_TravelorCallFlag + " ," + VisitModel.Visit_ID + " AS " + VisitModel.Visit_ActionTaken + " ," + VisitModel.Visit_CheckInDate + " AS " + VisitModel.Visit_Createdat + " ," + VisitModel.Visit_CheckOutDate + " AS " + VisitModel.Visit_LastModifiedat + " ," + VisitModel.Visit_EmployeeName + " AS " + VisitModel.Lat + " ," + VisitModel.Visit_Createdby + " AS " + VisitModel.Long + " FROM " + VisitModel.Visit_Table + " WHERE " + VisitModel.Visit_IncidentID + " =" + id + " and strftime('%Y-%m-%d', " + VisitModel.Visit_CheckInDate +") = '" + date +"' " + " UNION ALL " + " SELECT " + VisitModel.Visit_TravelorCallFlag + " ," + VisitModel.Visit_TravelID + " AS " + VisitModel.Visit_ActionTaken + " ," + VisitModel.Visit_CheckInDate + " AS" + VisitModel.Visit_Createdat + " ," + VisitModel.Visit_CheckOutDate + " AS " + VisitModel.Visit_LastModifiedat + " ," + VisitModel.Visit_StartLat + " AS " + VisitModel.Lat + " ," + VisitModel.Visit_EndLat + " AS " +VisitModel.Long +" FROM " + VisitModel.Visit_TravelTable + " WHERE " + VisitModel.Visit_TravelIncidentID + " =" + id + " and strftime('%Y-%m-%d', " + VisitModel.Visit_CheckInDate +") = '" + date +"' )" + " order by date (" + VisitModel.Visit_Createdat + " ) DESC " ;
Cursor cursor = db.rawQuery(visitquery ,null);
List<VisitModel>lisobj=new ArrayList<>();
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
VisitModel visitobjs= new VisitModel();
visitobjs.setStartLat(cursor.getString(cursor.getColumnIndex(VisitModel.Lat)));
visitobjs.setStartLong(cursor.getString(cursor.getColumnIndex(VisitModel.Long)));
/* String kil=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_Kilometer));
visitobjs.setKilometer(kil);
String time=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_CheckInDate));
visitobjs.setCheckInDate(time);
String nextdate=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_CustOffDate));
String endlat=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_CallSlipNo ));
visitobjs.setEndLat(endlat);
visitobjs.setNextVisitDate(nextdate);
String startlat=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_Distance));
visitobjs.setStartLat(startlat);
String empname=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_EndLong));
visitobjs.setEmployeeName(empname);*/
String checkin=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_Createdat));
String checkout=cursor.getString(cursor.getColumnIndex(VisitModel.Visit_LastModifiedat));
visitobjs.setCheckInDate(checkin);
visitobjs.setCheckOutDate(checkout);
int flag=cursor.getInt(cursor.getColumnIndex(VisitModel.Visit_TravelorCallFlag));
int primarykey=cursor.getInt(cursor.getColumnIndex(VisitModel.Visit_ActionTaken));
visitobjs.setID(primarykey);
visitobjs.setTravelrVisitFlag(flag);
lisobj.add(visitobjs);
}
while (cursor.moveToNext());
}
}
cursor.setNotificationUri(mcontext.getContentResolver(), VisitDAO_URI);
return lisobj;
}
得到:`第一个ORDER BY术语与结果集中的任何列都不匹配
任何人都能告诉我解决方案是什么意思!
答案 0 :(得分:2)
而不是
" order by date (" + VisitModel.Visit_CheckInDate + " ) DESC "
尝试
" order by date (" + VisitModel.Visit_Createdat + " ) DESC "
当您使用UNION组合多个查询时,将对整个结果集进行排序,因此您必须使用结果集中的相同列。 在这种情况下,这意味着您必须提供列的别名。
编辑试试这个:
String visitquery=
"SELECT * FROM (
Select " + VisitModel.Visit_TravelorCallFlag + " ," + VisitModel.Visit_ID + " AS " + VisitModel.Visit_ActionTaken + " ," + VisitModel.Visit_CheckInDate + " AS " + VisitModel.Visit_Createdat + " ," + VisitModel.Visit_CheckOutDate + " AS " + VisitModel.Visit_LastModifiedat + " ," + VisitModel.Visit_EmployeeName + " AS " + VisitModel.Lat + " ," + VisitModel.Visit_Createdby + " AS " + VisitModel.Long + " FROM " + VisitModel.Visit_Table + " WHERE " + VisitModel.Visit_IncidentID + " =" + id + " and strftime('%Y-%m-%d', " + VisitModel.Visit_CheckInDate +") = '" + date +"' " +
" UNION ALL " +
" SELECT " + VisitModel.Visit_TravelorCallFlag + " ," + VisitModel.Visit_TravelID + " AS " + VisitModel.Visit_ActionTaken + " ," + VisitModel.Visit_CheckInDate + " AS" + VisitModel.Visit_Createdat + " ," + VisitModel.Visit_CheckOutDate + " AS " + VisitModel.Visit_LastModifiedat + " ," + VisitModel.Visit_StartLat + " AS " + VisitModel.Lat + " ," + VisitModel.Visit_EndLat + " AS " +VisitModel.Long +" FROM " + VisitModel.Visit_TravelTable + " WHERE " + VisitModel.Visit_TravelIncidentID + " =" + id + " and strftime('%Y-%m-%d', " + VisitModel.Visit_CheckInDate +") = '" + date +"' " + "
)order by 3 DESC " ;
答案 1 :(得分:0)
请使用以下示例,在使用union
合并两个查询后按列号完成排序SELECT * FROM (
SELECT * FROM TABLE_1
UNION
SELECT * FROM TABLE_2 )
ORDER BY 2 DESC