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)
8
  • Can you print out the SQL after the variables have been substituted? And are you sure there is a column called descriptions in the characters table? Jan 19, 2014 at 22:13
  • I am 100% sure, that the column "descriptions" exists. I edited the main post with a substituted sql ;) thx Jan 19, 2014 at 22:19
  • What if in your arr or s you have a value containing ' ? OK, maybe you don't. Jan 19, 2014 at 22:21
  • Like you can see in the substituted sql the error occurs even if the variable don't contain ' Jan 19, 2014 at 22:23
  • Try giving an alias to your case statement maybe?! Jan 19, 2014 at 22:23

3 Answers 3

8

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
3
  • 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. Jan 19, 2014 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. Jan 19, 2014 at 23:24
  • @PhilPlückthun Yes, the first term had the same problem; updated.
    – CL.
    Jan 20, 2014 at 8:32
0

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.