Я использовал следующий код для запроса одной таблицы:
public Cursor fetchFilteredItemsNearSortedByDistance(String strTable, String[] strArrayFields, String strField, String strCriterion, double dblCentreEasting, double dblCentreNorthing, double dblRadius) {
String strSQL = "Easting > " + Double.toString(dblCentreEasting-dblRadius);
strSQL += " AND Easting < " + Double.toString(dblCentreEasting+dblRadius);
strSQL += " AND Northing > " + Double.toString(dblCentreNorthing-dblRadius);
strSQL += " AND Northing < " + Double.toString(dblCentreNorthing+dblRadius);
strSQL += " AND " + strField + " LIKE '%" + strCriterion + "%'";
strSQL += " ORDER BY ";
strSQL += "((Easting - " + Double.toString(dblCentreEasting)+")";
strSQL += " * (Easting - " + Double.toString(dblCentreEasting)+")";
strSQL += " + (Northing - " + Double.toString(dblCentreNorthing)+")";
strSQL += " * (Northing - " + Double.toString(dblCentreNorthing)+"))";
strSQL += " COLLATE NOCASE";
return myDbHelper.myDataBase.query(strTable, strExtendedArrayFields, strSQL, null, null, null, null);
}
Приведенный выше код работал удовлетворительно.Тем не менее, теперь я хочу распространить идею на UNION из двух или более таблиц, и попробовал следующий код:
public Cursor fetchFilteredPOIsNearSortedByDistance(String strTable, String[] strArrayFields, String strField, String strCriterion, double dblCentreEasting, double dblCentreNorthing, double dblRadius) {
String strSQL = "SELECT * FROM TableA ";
strSQL += "UNION SELECT * FROM TableB ";
strSQL += "WHERE ( Easting > " + Double.toString(dblCentreEasting-dblRadius);
strSQL += " AND Easting < " + Double.toString(dblCentreEasting+dblRadius);
strSQL += " AND Northing > " + Double.toString(dblCentreNorthing-dblRadius);
strSQL += " AND Northing < " + Double.toString(dblCentreNorthing+dblRadius);
strSQL += " AND " + strField + " LIKE '%" + strCriterion + "%' ) ";
strSQL += " ORDER BY ";
strSQL += "((Easting - " + Double.toString(dblCentreEasting)+")";
strSQL += " * (Easting - " + Double.toString(dblCentreEasting)+")";
strSQL += " + (Northing - " + Double.toString(dblCentreNorthing)+")";
strSQL += " * (Northing - " + Double.toString(dblCentreNorthing)+"))";
strSQL += " COLLATE NOCASE";
return myDbHelper.myDataBase.rawQuery(strSQL, null);
}
Однако, это создает «SQLiteException: 1-й термин ORDER BY не соответствует ни одномустолбец в наборе результатов "ошибка.
Полная ошибка в LogCat:
ERROR/AndroidRuntime(11444): Caused by: android.database.sqlite.SQLiteException: 1st ORDER BY term does not match any column in the result set: , while compiling: SELECT * FROM TableA UNION SELECT * FROM TableB WHERE ( Easting > 594378.8427734375 AND Easting < 663822.8427734375 AND Northing > 127586.046875 AND Northing < 197030.046875 AND Name LIKE '%%' ) ORDER BY ((Easting - 629100.8427734375) * (Easting - 629100.8427734375) + (Northing - 162308.046875) * (Northing - 162308.046875)) COLLATE NOCASE
Может кто-нибудь помочь мне на правильном пути, пожалуйста?