I have two tables:
atvtrails_prim:
ID int PK
ID2 int
TType varchar(100)
atvtrails_sec:
ID2 int Foreign Key(Relates to ID2 of atvtrails_prim)
Latitude double
Longitude double
What I want from these tables: All the records in atvtrails_sec whose ID2 has a specific TType in atvtrails_prim.
Query I am using(Nested SELECT):
SELECT atvtrails_sec.ID2,atvtrails_sec.Latitude, atvtrails_sec.Longitude,atvtrails_prim.TType
FROM atvtrails_sec,atvtrails_prim
WHERE atvtrails_sec.Latitude BETWEEN %@ and %@
AND atvtrails_sec.Longitude BETWEEN %@ and %@
AND (
SELECT TType from atvtrails_prim where ID2=atvtrails_sec.ID2
) =%@", lrLat, ulLat, lrLong, ulLong,type2
lrLat, ulLat, lrLong, ulLong,type2 are variables. I am coding in objective-c.
This query gives me a very large dataset. It does give me the correct ones but they are being duplicated. I get around 90000 results while the table has only 18000 records. This query is taking a lot of time when I measured and I want a faster solution.
Query I am using(INNER JOIN):
SELECT atvtrails_sec.ID2,atvtrails_sec.Latitude, atvtrails_sec.Longitude
FROM atvtrails_sec
INNER JOIN atvtrails_prim ON atvtrails_prim.TType=%@
WHERE atvtrails_sec.Latitude BETWEEN %@ and %@
AND atvtrails_sec.Longitude BETWEEN %@ and %@", lrLat, ulLat, lrLong, ulLong,type1
This is not returning me any data. I am new to SQLite. Kindly guide me as to how can I get the desired data using Inner-Join. Also, is there any way to optimize my nested SELECT query so that it doesn't give me extra data?
{}icon to preserve code formatting. Also try to format the code in a readable way. Finally, are you using mysql or sqlite? Delete the improper tag. - Hart CO