Missing values after joining tables in SQLite - android

This is relationship between tables in SQLite database. There I am trying to join 5 tables.
SELECT * FROM r_ele
WHERE r_ele.value LIKE 'でも%'
Query above returns this result(which is ok). The table next to this missing words in red!
But when I try join tables, some values are missing.
SELECT e.id AS entry_id,
re.value AS re_value,
GROUP_CONCAT(DISTINCT ke.value) AS ke_value,
GROUP_CONCAT(DISTINCT g.value) AS g_value
FROM (entry e
INNER JOIN k_ele ke ON e.id = ke.fk
INNER JOIN r_ele re ON e.id = re.fk
INNER JOIN sense s ON e.id = s.fk
INNER JOIN gloss g ON s.id = g.fk)
WHERE g.lang IS NULL AND re_value LIKE 'でも%'
GROUP BY re.value
ORDER BY re_value;
Result:
What I am doing wrong? What should I do in order to avoid missing some values from table 'r_ele'?

Change INNER JOIN to LEFT JOIN to include all records from the left table, even if there are no matching records in the right table.

Related

SQLite Android Join operation on FTS4

Table1 is Virtual Table with fts4 and TABLE2 is normal table.
Query1 (WORKS)
SELECT * FROM TABLE1 LEFT OUTER JOIN TABLE2 ON TABLE1.id=TABLE2.id WHERE TABLE1 MATCH 'sometext' LIMIT %d,%d
Query 2 (DOES NOT WORK)
SELECT * FROM TABLE2 LEFT OUTER JOIN TABLE1 ON TABLE1.id=TABLE2.id WHERE TABLE1 MATCH 'sometext' LIMIT %d,%d
Error from Query2
android.database.sqlite.SQLiteException: unable to use function MATCH
in the requested context (code 1)
From this it seems like FTS tabl need to be first on LEFT OUTER JOIN. Why this happening? Join happens first before applying WHERE clause. so if it was not working for table type mismatch i was assuming it would not work with query1 either. Anyone please explain this. what does happening internally? Also Any link to reference site would be appreciated explaining this.
MATCH works only on the FTS table itself, so it must be executed before the join.
In the second query, the database looks up matching rows first (with an outer join, it has no choice in the join order), and that temporary result is no longer an FTS table.
When using FTS, it usually is a better idea to move the FTS search (or the other search) into a subquery:
SELECT *
FROM Table2 LEFT JOIN (SELECT *
FROM Table1
WHERE Table1 MATCH ...)
USING (id);

Android : sqlite join result

I have a table which has one column's name and guestof .
I want to return a cursor to get three values : name guestOf and guestcount where guestcount is how many rows has "name" as the value of guestOf.
How can combine this internal query and return the result. Trying something like this, can it be done without inner query for better performance
SELECT name, guestCount from Table
as A outer join (SELECT guestOf , count(*)
AS GuestCount FROM Table group by guestOf)
as GuestCounts WHERE A.id = GuestCounts.guestOf
Give this code a go. I think it matches what you're looking for:
SELECT g.name, g.guestof,
(SELECT COUNT(guestof)
FROM guests AS c
WHERE g.name == c.guestof
) as guestcount
FROM guests AS g;
And as an example: SQLFiddle

sqlite delete statement with JOIN an UNION

I have to do a complicated delete-statement, that looks like this:
DELETE FROM
(Select t1._id, t1.subject, t1.predicate, t1.object from t1
INNER JOIN t1 AS t2 ON t1.subject=t2.subject
WHERE t2.object='ID20447_325762212'
UNION
Select t1._id, t11.subject, t1.predicate, t1.object from t1
INNER JOIN t1 AS t2 ON t1.object=t2.subject
WHERE t2.object='ID20447_325762212')
SQLitedabatase#delete(Table, selection, selectionArgs) is not useable I think. No way to have Joins and Unions.
SQLiteQueryBuilder has JOINs with setTables(joinString) and UNIONs with subQuery() and Unionquery, but only for selectqueries not delete.
How does it work?
[update]
I could reduce complexity by separating both subqueries in two delete-statements, but there is a join in everyone so thats not a solution. And that's not working either.
[update2]
Here's a screenshot from rows of my table i want to delete.
In my app I have an object with ID...2328 (row 2) and a property(relation) represented by the rows in that screenshot. The relation is targeting to an object with id ...212 . I have to delete that object and all Properties
You can use complex selections in subqueries.
In the general case, it would look like this:
DELETE FROM SomeTable WHERE _id IN (SELECT _id FROM ...complex query...)
However, this particular query isn't very complex after all:
DELETE FROM t1
WHERE subject = (SELECT subject FROM t1 WHERE object = 'ID20447_325762212')
OR object = (SELECT subject FROM t1 WHERE object = 'ID20447_325762212')

Join two SQLite tables in Android application

I need to join two tables into a relational table. I know that I can do this with inner join but I don't know how to start.
TABLE_SOCIO
-----------------
-id
-name
-phone
-email
TABLE QUOTA
-----------------
-id
-name
-description
-value
Now I need to join this two in on unique table by the id's of each.
RELATIONAL TABLE
-----------------
-id_Quota
-id_Socio
I have one id_Quota to n id's_Socio. It's a relation 1-n. I have an array list with the set of id_Socio that I need to save in relational table with only one id_Quota.
Any ideas? How I can start with inner join? Foreign key it's need?
Any example?
SELECT t1.name, t1. phone,
t2.description, t2.value
FROM TABLE_SOCIO t1
INNER JOIN RELATIONALTABLE r ON t1.Id = r.id_Socio
INNER JOIN TABLE_QUOTA t1 ON t2.Id = r.id_Quota

How to union 3 tables and join it with another one in SQLite

So I have three tables, each with different columns and one superclass table. I have a unique id for each entity that's in the superclass table and its own table.
I want to union the three tables and then join the result with the superclass table based on the id.
How do I go about this? I only have the unique id and no other information.
Edit:
I tried what you suggested but got this exception:
08-01 18:49:05.365: E/AndroidRuntime(10737): Caused by:
android.database.sqlite.SQLiteException: SELECTs to the left and right of UNION do not
have the same number of result columns: , while compiling: SELECT * FROM ((SELECT * FROM
pois UNION SELECT * FROM routes UNION SELECT * FROM events) t INNER JOIN features ON (pid
= t.features_pid)) WHERE pid = "poi2114"
Nested queries can serve as tables in the FROM clause.
select * from (
select ID from TableA
union
select ID from TableB) ab inner join TableX x on ab.ID = x.FooID
The field names in the constructed table ab follow the names in the first select that constitutes ab. So if the field in TableB is called something else, it will still be under the ID column. That's how UNION queries work.
Also, be mindful of the UNION ALL/UNION DISTINCT distinction.

Categories

Resources