SQLite Forum

How do I do a one - many join?
Login

How do I do a one - many join?

(1) By David Jackson (davidjackson) on 2021-07-26 20:25:29 [link] [source]

How am trying to combine tables stations and detail. I want to use the longitude and latitude files for the join. I want all the records from stations and all the matching records from detail. Here is the SQL I am trying to run:

SELECT s.station_name,s.start_lat,s.start_lng,started_at,ended_at,duration FROM stations AS s,detail AS d WHERE s.start_lat = d.start_lat AND WHERE s.end_lat = d.ended_at

TX, David

(2.1) By Harald Hanche-Olsen (hanche) on 2021-07-26 20:41:14 edited from 2.0 in reply to 1 [source]

AND WHERE is a syntax error. Replace by AND.

Anyhow, I think your query is better written as a join:

SELECT s.station_name, s.start_lat, s.start_lng, started_at, ended_at, duration
FROM stations AS s
JOIN detail AS d
ON (s.start_lat = d.start_lat AND s.end_lat = d.ended_at);