I have two tables that are connected via a Foreign Key. In table A I’m trying to get all the rows where the data in the time column is greater than or equal to the table B start column and where the data in the time column is less than or equal to the table b end column. I was thinking that an inner join might work for this, however, the results are not what I expected. It is returning some data but they are repeating themselves.
Here is what I have:
SELECT * FROM columnA
INNER JOIN columnB
ON columnA.time >= columnB.start
OR columnA.time <= columnB.stop
Am I looking at this wrong? Is an inner join even the right way of collecting this data?
UPDATE:
Actually tableA’s Foreign Key is connecting to the Primary Key on tableB. Anyways I did this:
SELECT * FROM tableA
INNER JOIN tableB
ON tableA.time >= tableB.start
AND tableA.time <= tableB.stop
The reason I didn’t do what you did for the on is because the data has been in the database for a couple years now and I just recently added the column with the foreign key. So that column doesn’t have any data in it yet. That’s why I’m querying it so I can know which columns to update with the information. It returned the data to me but also returned other data that didn’t meet the requirements.
I managed to figure out the solution by adding a WHERE to match it the data in the exact row I was looking for. Here is my solution: