8

I've got a small problem with an SQL cursor. I'm trying to execute an SQL command on android. I'm trying to sort the returned content by cases, but it seems like the system doesn't accept the returned values (?!) I've tried everything! Do you have the solution ? ;)

cursor = db.rawQuery(c, null);
String c = "SELECT * FROM characters WHERE UPPER(descriptions) LIKE '%" + 
    TextUtils.join("%", arr) + "%' UNION ALL SELECT * FROM words WHERE
    UPPER(descriptions) LIKE '%" + TextUtils.join("%", arr) + "%'
    ORDER BY CASE WHEN UPPER(descriptions) LIKE '" + s + "' THEN 1
    WHEN UPPER(descriptions) LIKE '" + s + "|%' THEN 2 WHEN
    UPPER(descriptions) LIKE '%|" + s + "|%' THEN 2 WHEN UPPER(descriptions)
    LIKE '%|" + s + "' THEN 2 WHEN UPPER(descriptions)
    LIKE '%" + TextUtils.join(" ", arr) + "%' THEN 3 ELSE 4 END, descriptions ASC";

Do I need to sort the command like this?

SELECT * FROM characters WHERE UPPER(descriptions) LIKE '%" + TextUtils.join("%", arr) + "%' 
UNION ALL
SELECT * FROM words WHERE UPPER(descriptions) LIKE '%" + TextUtils.join("%", arr) + "%'                                     
ORDER BY
    CASE
        WHEN UPPER(descriptions) LIKE '" + s + "' THEN 1          
        WHEN UPPER(descriptions) LIKE '" + s + "|%' THEN 2 
        WHEN UPPER(descriptions) LIKE '%|" + s + "|%' THEN 2 
        WHEN UPPER(descriptions) LIKE '%|" + s + "' THEN 2 
        WHEN UPPER(descriptions) LIKE '%" + TextUtils.join(" ", arr) + "%' THEN 3 
        ELSE 4 
    END,
descriptions ASC

Thanks ;)

EDIT: An SQL command after inserting the search term "a test"

SELECT * FROM characters WHERE UPPER(descriptions) LIKE '%A%TEST%'
UNION ALL
SELECT * FROM words WHERE UPPER(descriptions) LIKE '%A%TEST%'
ORDER BY
    CASE
        WHEN UPPER(descriptions) LIKE 'A TEST' THEN 1
        WHEN UPPER(descriptions) LIKE 'A TEST|%' THEN 2
        WHEN UPPER(descriptions) LIKE '%|A TEST|%' THEN 2
        WHEN UPPER(descriptions) LIKE '%|A TEST' THEN 2
        WHEN UPPER(descriptions) LIKE '%A TEST%' THEN 3
        ELSE 4
    END,
descriptions ASC

table schema:

characters (t TEXT,s TEXT,jy TEXT,descriptions TEXT)
words (t TEXT,s TEXT,jy TEXT,descriptions TEXT)
Phil Plückthun
  • 1,381
  • 9
  • 14

3 Answers3

7

When you are SELECTing records from a single table, you can use anything from those records for sorting.

However, when you are combining multiple queries with UNION, the sorting is done on the entire result, so you must use some column from the result for ordering. In this case, this means that you must move the calculation into the query itself:

SELECT t, s, jy, descriptions, CASE ... END AS ordernr FROM ...
UNION ALL
SELECT t, s, jy, descriptions, CASE ... END AS ordernr FROM ...
ORDER BY ordernr,
         descriptions
CL.
  • 173,858
  • 17
  • 217
  • 259
  • I think the problem still lies in the CASE ... END part. Even if I replace the order by descriptions command with '4', or just omit it the error stays. The error states also "(1) 1st ORDER BY term...", so it clearly states a problem in the first part. – Phil Plückthun Jan 19 '14 at 22:48
  • @CL. . . . Your description is just clearly wrong. `Union` (and `union all`) take the column names from the first table/query in the union. The `order by` at the end of the statement then applies to the union'ed results. – Gordon Linoff Jan 19 '14 at 23:24
  • @PhilPlückthun Yes, the first term had the same problem; updated. – CL. Jan 20 '14 at 08:32
0

See this post and try specifying all columns in your select statements.

Community
  • 1
  • 1
AlexS
  • 5,295
  • 3
  • 38
  • 54
0

The real reason seems here :

In standard SQL, the ORDER BY clause must be either (1) a positive integer that means sort by the corresponding column or (2) an exact copy of one of the expressions that defines the column. The ORDER BY may not have arbitrary expressions, in standard SQL. Many RDBMSes (including SQLite) extend this so that you can have arbitrary expressions in the ORDER BY clause, but in the case of a UNION query, that enhancement does not apply (for technical reasons) and the original standard-SQL restriction is in force.

gillescpp
  • 16
  • 1