Join two SQLite tables in Android application - android

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

Related

SQLite INNER JOIN - How to output only one of the common columns the two tables have

Let's say that I have two tables:
lessons with columns: lessonID, name
studentlessons with columns: lessonID, age
I am trying to perform this action:
SELECT lessons.*, studentlessons.* FROM studentlessons
JOIN lessons WHERE studentlessons.lessonID = lessons.lessonID
but the result is a table which have the columns:
lessonID, name, lessonID(1), age
I want to avoid the lessonID(1), so my desired output must be:
lessonID, name, age
I know that I can use this syntax:
SELECT lessons.lessonID, lessons.name, studentlessons.age FROM studentlessons
JOIN lessons WHERE studentlessons.lessonID = lessons.lessonID
but I can't because of some other reasons.
Is there any purely SQLite syntax that can give me my desired output?
What you want is a NATURAL JOIN:
SELECT * FROM studentlessons NATURAL JOIN lessons
which returns only one of the columns lessonID.
The tables are joined implicitly on the columns that have the same name(s).
See the demo.

joining two table in sqlite

i have two table
and table two data is
and i want to show just like this
I have to make a table out of two other tables one table with column word and serial and another with column meaning
and serial.
Now what I have to do is put this result(data)
into table3 (a table i already have, with the same columns as in table1 and (table2) tried lots but can't please help me
I hope this isn't too confusing of a question.
select
a.serial,
b.word,
a.word as meaning
from tbl1 a
join tbl2 b on a.serial = b.serial
This is the sql query your looking for ,details related to the same is been provided in the link below
INSERT INTO table3 (serial,word,meaning)
SELECT t1.serial,t2.word,t1.word
FROM table1 as t1
JOIN table2 as t2 ON t1.serial=t2.serial
click here w3schools SQL tutorial
Try
select * from eng join other on eng.serial=other.serial

Missing values after joining tables in SQLite

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.

How to add data from table of one database into table of another database in SQLite

Suppose I have two database files a.sqlite and other one is b.sqlite. suppose table1 is in a.sqlite and table2 in b.sqlite. I open a.sqlite in read-only mode and b.sqlite in read-write.Suppose both table table1 and table2 has same column name "description".table2 has description column with all null values and table1 has some values.So how can i add the data from table1 into table2.I know through query.But as these r in two different databse,so is there ant problem? Can any one suggest ?
Not sure if your environment allows it, but ATTACH DATABASE is normally the way to go:
http://www.sqlite.org/lang_attach.html

SQL: Cross table with foreign keys to two different tables

I have three tables:
zip_code_data
|zipCodeId| primary key
|zipCode| indexed
|other columns...|
location_data
|locationDataId| primary key
|city| indexed
|other columns...|
x_data
|id| primary key
|zipCodeId| foreign key
|locationDataId| foreign key
My goal is to run a query for either zipcode or city, and get all of the data associated with it from the zip_code_data and location_data tables
For example, if a user searches for a zipcode, I want to pull back all of the data associated with that zipcode from both tables.
My first guess is to get the foreign keys first from the cross table (x_data, example below) and then use those to get the data from each respective table... Since i'm somewhat of a novice user I don't know the best way to do this.
SELECT x_data.zipCodeId, x_data.locationDataId
FROM x_data
INNER JOIN zip_code_data
ON x_data.zipCodeId=zip_code_data.zipCodeId
WHERE zip_code_data.zipCode LIKE '2322%'
You could create an inline view:
select zips.othercolumn, LOCS.city
from zips
inner join x_data on zips.zip = x_data_zip and zips.zip like .....
left join (
select id, locations.city from locations
where locations.id = x_data.locationid
) as LOCS
or just join the locations table:
left join locations as locs on locs.locationid = x_data.locationid
I was about to post:
SELECT zip.*,loc.*
FROM x_data xref
JOIN zip_code_data zip ON zip.zipCodeId=xref.zipCodeID
JOIN location_Data loc ON loc.locationDataID=xRef.locationDataID
WHERE zip.zipCode LIKE '2322%' or loc.city LIKE '%aaa%'
but it looks like you've already got it...

Categories

Resources