Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android sql - how do you order your sql query by multiple columns

Tags:

android

I am currently working on an Android project in Eclipse and i am having problems with my SQL query.

I am trying to order the query by more than two columns, currently i am doing it by KEY_DAY_ID but i want to also do it by KEY_START_TIME, but i can't get it to work

my query currently looks like this:

Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE, 
            KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
            null, null, null, null, KEY_DAY_ID  + " ASC");

Please let me know your thoughts. Thank you in advance!

like image 747
sailor2468 Avatar asked Apr 20 '12 22:04

sailor2468


People also ask

How do I ORDER BY multiple columns in SQL?

Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first. The column that is entered at first place will get sorted first and likewise.

Can we ORDER BY two columns in SQL?

After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.

How do I order columns in SQL?

Using SQL Server Management Studio In Object Explorer, right-click the table with columns you want to reorder and select Design. Select the box to the left of the column name that you want to reorder. Drag the column to another location within the table.

Can ORDER BY clause work on multiple columns?

You can also ORDER BY two or more columns, which creates a nested sort . The default is still ascending, and the column that is listed first in the ORDER BY clause takes precedence. The following query and Figure 3 and the corresponding query results show nested sorts.


3 Answers

The last parameter in db.query() method is the order by clause (without the "order by"). All you need to do is separate both columns by a ",". So it would look like:

Cursor cursor = db.query(TABLE_SESSION, new String[] {KEY_ID, KEY_MODULE_CODE, 
        KEY_DAY_OF_WEEK, KEY_START_TIME, KEY_DURATION, KEY_SESSION_TYPE, KEY_ROOM},
        null, null, null, null, KEY_DAY_ID + " ASC, " + KEY_START_TIME  + " ASC");
like image 87
Kevin Westwood Avatar answered Oct 02 '22 08:10

Kevin Westwood


This works for me

SQLiteCursor cursor = (SQLiteCursor) db.query(DbHelper.TIMES, colmn, null, null, null, null, DbHelper.TABLE_DAY + " ASC, " + DbHelper.TABLE_LECTURE_NO + " ASC",null);
like image 29
msh1521962 Avatar answered Oct 02 '22 09:10

msh1521962


Also you can do it in select line like this:

Cursor data = ddbb.rawQuery("SELECT * FROM vacations ORDER BY NAME ,MONTH , date ",null);

in previous code the first probability for the first column "NAME" then will start arrange by the Second probability "MONTH" then the third "date"..... which mean working in series
Or:

Cursor data = ddbb.rawQuery("select * from vacations where NAME = ? ORDER BY MONTH AND date ",new String[]{ns});

in previous code by using "AND" the two conditions are working together in parallel

like image 35
Aly Abdelaal Avatar answered Oct 02 '22 09:10

Aly Abdelaal