Select from multiple tables shows no results if one is empty sqlite - android

I'm using this in android but using SQLite Studio 2.1.4 for testing.
I have two tables: A and B.
A has 2 records and B has no records.
If I query
select * from A
I get the results in that table, which is fine.
If I query
select * from B
I get no results, which is also fine.
However, if I query:
select * from A, B
I also get no results, which is wrong... I should get the results from A, right?
Or is SQLite somewhat different from the other DBMS out there?

SELECT * from A, B is a cartesian join of both tables. In other words every row in table A is joined to every row in table B. So if A has 5 rows and B has 10 rows, you end up with 50 rows in your results set. In this case, table B has zero rows hence it doesn't matter how many rows A contains, you get no rows back.
You will find the same behaviour in all relational databases.

Related

SQLite Android load rows based on ids, obtained from a string array in a different table?

I have a database table A, that contains two columns: the primary key _idA and a column called datalist.
I also have a database table B, containing multiple columns, one of them being its primary key _idB, the others contain misc data.
I want to load the datalist from table A by a specific _idA. This datalist is a string, containing _idBs separated by a comma, i.e. "31,62,612,6123,682". After that, I need to load all rows from table B, that have an _idB that is contained in the datalist of table A.
Since I am on Android, working with a Dataloader, that is connected to a Listview, if possible I would really like to do all this in one query, since I am not completely sure it is possible to do two separate queries in one Android DataLoader.
Example:
-> My program needs all items with an id that is stored in the datalist of _idA = 5
-> Get datalist from table A, where _idA = 5
-> Get all rows from table B, where _idB is in datalist from table A, _idA = 5
I am still pretty new to SQLite and still learning, so thanks a lot for your help!
Assuming that by DataLoader you are referring to the CursorLoader, things are simple:
You should either:
Extent a cursor loader and in onLoadInBackground() method do the first table query, get the id and use it in the second table query. Make sure to return the Cursor and swap it in the ListView adapter.
In the first cursor loader onLoadFinished() callback, get the id and use it to start a new loader for the second table cursor.
Let me know if you need more help.

Joining 3 tables with WHERE clause in Android

I have problem with SQL in Android (SQLite).
I have three tables in relation many-to-many, suppose A, B and C, each has 30 records. I want to join these tables and after that use WHERE clause. I have written something like this code
SELECT *
FROM A
JOIN B ON A.id=B.photoid
JOIN C ON B.urlid=C.id
WHERE C.type=[SOME VALUE]
I have also tried this
SELECT *
FROM A
LEFT OUTER JOIN B ON A.id=B.photoid
LEFT OUTER JOIN C ON B.urlid=C.id AND C.type=[SOME VALUE]
and this
SELECT *
FROM A
CROSS JOIN B
CROSS JOIN C
WHERE A.id=B.photoid AND B.urlid=C.id AND C.type=[SOME VALUE]
These statements don't work in Android. They return 300 records.
What should I change? Is it bug or I do something wrong?
Hard to tell without any additional data, but keep in mind that a JOIN operation will produce a row for each combination of a value in any of the tables involved. If you had 3 tables, each with 30 rows and you wouldn't be filtering any result, it should produce 27000 rows, but you're filtering by A.id=B.photoid and B.urlid=C.id, which might be producing a smaller set of rows.
In my opinion, that's not anything related to SQLite, but to your query instead. I'd try to make your set of values much smaller (for example, 5 per table) and run the same query, and see which results are being returned which shouldn't be, and try to modify your query to make it discard all those unwanted results.

Android SQLite getting records from one table based on another

Please excuse me if its a repeated question. I tried searching here and at google but I couldn't exactly find what I wanted.
I have got two tables A & B.
Table A Fields : id, name, description, rating.
Table B Fields : id, aId (linked to table A), customerId, recommended.
Table A contains my data items for which I'm storing average cumulative ratings provided by users.
Table B stores another attribute for data of Table A. It stores the recommended bit (1 for recommended & 0 for non-recommended).
I want to list all the data from Table A but I want to sort them using recommended bit from Table B. So, if there are 10 records in Table A and 2 records in Table B, while listing all those 10 records, the two from Table B should come first and then the others from Table A. It doesn't matter whether the recommended bit value is a 0 or a 1. While listing the other 8 records from Table A, I want to list the records based on their rating in descending order.
Can someone please guide me in writing this sqlite query for Android app? Thanks in advance!
The left join adds the recommended field to the result set (with a value of NULL if there is no matching B record).
The expression recommended IS NULL or EXISTS(...) returns either 0 or 1:
SELECT DISTINCT A.*
FROM A LEFT JOIN B ON A.id = B.aId
ORDER BY B.recommended IS NULL,
A.rating DESC
Alternatively:
SELECT *
FROM A
ORDER BY NOT EXISTS (SELECT 1
FROM B
WHERE B.aId = A.id),
rating DESC

SQLite: Multiple select statements vs one select statement

In my Android project, I have a SQLite table data containing two rows, id and name. It has +/- ten records. Note that records may have been deleted, so the ids could be non-sequential.
In an activity I need the corresponding names of +/- 100 ids (obviously with lots of duplicate values).
I could do this by executing the SQLite query SELECT name FROM data WHERE id = x a hundred times.
Another option is executing the SQLite query SELECT id,name FROM data one time, then store the ids and the names in an array, and then get the names of the ids by java (String name = namesArray[Arrays.binarySearch(idsArray, x)];
Which method should I use? Or is there even a better solution?
The second option is much faster.

Querying from multiple tables in Android SQLite?

I was wondering if it's possible (it should be) to query multiple tables simultaneously (several at once) in SQLite. Basically I have several tables that have the exact same columns, but the data in them is just organized by the table it's in. I need to be able to use SELECT to get data from the tables (I heard UNION could help), which matches a condition, then group the data by the table it's in.
In other words, would something like this be possible?
SELECT * FROM table1,table2,table3,table4,table5,table6 WHERE day=15 GROUP BY {table}
I'd rather not resort to having to query the tables individually as then I would have a bunch of Cursors that I'd have to manually go through and that would be difficult when I only have one SimpleCursorAdapter? Unless a SimpleCursorAdapter can have several Cursors?
Thanks.
EDIT: The structure of my tables:
Main Table - contains references to subtables in a column "tbls"
and meta-information about the data stored in the subtables
Subtable - contains reference to subsubtables in a column "tbls"
and meta-information about the data stored in the
subsubtables
Subsubtable - contains the actual entries
Basically these tables just make it easier to organize the hierarchical data structure. I suppose instead of having the subsubtables, I could keep the actual entries in the subtable but add a prefix, and have a separate table for the meta-information. It just seems it would be harder to delete/update the structure if I need to remove a level in this data set.
You can create view based on your tables, the query of your view is union of your tables.
create view test as select * from table1 union select * from table2
now you can filter data as you want
for more info check union & view
http://www.w3schools.com/sql/sql_union.asp
http://www.w3schools.com/sql/sql_view.asp
In the end, I decided to forgo having many subsubtables, and instead adding another column like Tim and Samuel suggested. It will probably be more efficient as well then chaining SELECTs with UNION.

Categories

Resources