Android Question SQL Query??

ilan

Expert
Licensed User
Longtime User
hi

for my app i need to perform a query but i am not sure how to do it.
i need to retrieve the record by a column but i allow the user to arrange it.

so i have a table where the user can get all categories (table2) and he is allowed to arrange the order how he like and i store it under catpos (column name)

then i have a table with items (table1) and i need to get all records in order of the other table catpos ASC
something like this:


1703663779379.png


i guess i need to use JOIN somehow right?
 
Last edited:

ilan

Expert
Licensed User
Longtime User
For complex sql queries joining tables etc i ask chatgpt after posting tables structure. It gives quite good answers
i tried what you say

i asked:

write me a query to get a record from one table order bey a column and another table


and this is what chatgpt answered:

B4X:
SELECT t1.*
FROM table1 AS t1
JOIN table2 AS t2 ON t1.common_column = t2.common_column
ORDER BY t2.another_column_to_order_by;

🤔 not bad at all. chatgpt gave me the correct answer. very useful
 
Upvote 0

emexes

Expert
Licensed User
B4X:
ORDER BY t1.column_to_order_by, t2.another_column_to_order_by;

Lol you almost beat me to what I was in middle of writing, which was to consider adding t1.name as a secondary sort column eg:

SQL:
SELECT t1.* FROM "table1" AS t1 JOIN "table2" AS t2 on t2.category= t1.category ORDER BY t2.catpos, t1.name
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
sql is really fun

Since you are having fun with SQLite here is another way of writing the syntax. You do not need the word 'AS' and you can replace the JOIN with a comma, and you do not need the double quote around the table name:
B4X:
SELECT t1.* FROM table1 t1  , table2 t2  ON t1.category=t2.category ORDER BY t2.catpos
 
Upvote 0
Top