Android: Several orderBy statements - android

Is it possible to make two orderBy statements in one query. Say i wanna order my data after column 1 and afterwards after column 2. What would be the best way to do that?
Sincerely
Jesper

You just say ORDER BY Column1, Column2. If you are using the orderBy argument of one of the query methods, then remove the prefixing ORDER BY as it is inserted for you when building the query.

Related

Sqlite order by rows order

I am using this query
"select * from SomeTable group by SomeColumn"
It is returns list with accenting order, but i need to same order like in database.
For example the order in database is:
p
a
s
But result is:
a
i
p
Sample
The result need to be like distinct by CityEN but with all columns and order like 1.Paris 2.Amsterdam 3.Istanbul
In Sqlite, each row of a table has a unique rowid, which you can use for sorting.
select * from SomeTable group by SomeColumn order by rowid;
In your statement, add this line to sort the results:
order by min(rowid)
Your query does not enforce any order with ORDER BY clause so no assumption about row order should be made. If you want specific order add i.e. ORDER BY SomeColumn. See docs about all available order options: https://www.sqlite.org/lang_select.html#orderby
By the rules of SQL, you can't count on getting records back in any specific order without specifying an ORDER BY clause in your SQL query.
In practice servers sometimes return values in the order in which they're inserted, in the order of the first index created, or in the order of the primary key--but you can't count on this behavior, and in fact I've seen the behavior change between database maintenance windows or after the database version is upgraded. You definitely wouldn't want to count on a DB engine to give you back records in any particular order if you write a SELECT statement without an ORDER BY clause.
The only real way to get your records back in the order you inserted them is to create a timestamp column and then sort on it during the SELECT. If you don't want to worry about populating that column on INSERT, have that column auto-populate itself with a timestamp (depending on your DB engine).

Couchbase lite 2.0.0-DB021, Android, - OrderBy multiple columns?

How can I translate the following Orderby sql statement to
ORDER BY Country ASC, CustomerID DESC;
I have manage to write Ordering.property("Country").ascending();
but not sure how to add the next condition.
Thanks
If you look at the orderBy method, you will see that it has the following signature
public OrderBy orderBy(Ordering... orderings)
Note that you can put multiple Ordering objects inside.

Android Delete Query

I'm attempting to delete all rows in one table that do not have a corresponding ID in another table. Since apparently SQLite does not support joins in deletes I am trying to do something along these lines:
DELETE FROM my_table WHERE my_id NOT IN (SELECT _id FROM my_table2);
However, I apparently can not use rawQuery since it returns a cursor so I have to use the delete function. I'm having some trouble getting this working. Here is the query I'm trying:
mDb.delete("my_table", "my_id NOT IN ?", new String[]{"(SELECT _id FROM my_table2)"});
Thanks.
You shouldn't use a .rawQuery, as you stated, but you can use .execSQL() to accomplish it. I regularly use it for deletions myself.
I think the only way is to execute your select and dynamically combine your WHERE clause.
You have to use execSQL instead rawQuery because rawQuery is used for the statement that return data and execSQL is used for the statements that don't return data like DELETE

sql order by clause

Ok so in my database I have,
1,2,3,4
But what I really want is,
2,1,4,3.
so how do I order a column against a predefined set of sequence of strings or values?
Create a order by column, and order the results based on that.

How to return unique rows within a column in Android SQLite?

Really all I want to do is say the query SELECT DISTINCT column FROM table but I can't figure out how to structure it in the enourmous query methods that are part of SQLiteDatabase
I'm just trying to get the names of all contacts in a table.
You seem to know the sql query you want to run. Have you tried using rawQuery()?
You will probably find this version of the SQLiteDatabase#query method most useful.

Categories

Resources