I’m trying to JOIN two tables but it isn’t working properly. I have loc_id in all tables and they all have a value of 1. My database is INNODB. loc_id is the Unique ID for a third table called b_locations. I had tried including this table on JOIN as well but I don’t really need it and that also gave me the same results. I’d like to be able to JOIN 3 or more tables for future proofing, but I need to get 2 working first obviously.
Is my approach of joining on a Unique ID of a table not included in the query a bad approach?
Here is my SQL:
SELECT d.view_id,
d.gps_lat survey_lat,
d.gps_lng survey_lng,
d.thumbnail,
c.sign_type,
c.gps_lat object_lat,
c.gps_lng object_lng
FROM `d_view_angles` AS d
JOIN c_survey_elements AS c
ON d.loc_id = c.loc_id
WHERE c.loc_id = '1'
Here is what I am getting (See bold text below desired results):
1 37.367156 -77.39987 ... FREESTANDING SIGN 37.367229 -77.399835
2 37.367305 -77.399801 ... FREESTANDING SIGN 37.367229 -77.399835
3 37.36739 -77.400022 ... FREESTANDING SIGN 37.367229 -77.399835
4 37.367619 -77.399897 ... FREESTANDING SIGN 37.367229 -77.399835
1 37.367156 -77.39987 ... DOWN LIGHTING 37.367408 -77.400077
2 37.367305 -77.399801 ... DOWN LIGHTING 37.367408 -77.400077
3 37.36739 -77.400022 ... DOWN LIGHTING 37.367408 -77.400077
4 37.367619 -77.399897 ... DOWN LIGHTING 37.367408 -77.400077
1 37.367156 -77.39987 ... DOWN LIGHTING 37.367635 -77.399944
2 37.367305 -77.399801 ... DOWN LIGHTING 37.367635 -77.399944
3 37.36739 -77.400022 ... DOWN LIGHTING 37.367635 -77.399944
4 37.367619 -77.399897 ... DOWN LIGHTING 37.367635 -77.399944
I have tried GROUP BY survey_lat, survey_lng but it takes the top 4 results which gives me the following (notice the repeating c.sign_type, c.gps_lat and c.gps_lng):
1 37.367156 -77.39987 ... FREESTANDING SIGN 37.367229 -77.399835
2 37.367305 -77.399801 ... FREESTANDING SIGN 37.367229 -77.399835
3 37.36739 -77.400022 ... FREESTANDING SIGN 37.367229 -77.399835
4 37.367619 -77.399897 ... FREESTANDING SIGN 37.367229 -77.399835
These are the results I should be getting:
1 37.367156 -77.39987 ... FREESTANDING SIGN 37.367229 -77.399835
2 37.367305 -77.399801 ... FREESTANDING SIGN 37.367229 -77.399835
3 37.36739 -77.400022 ... DOWN LIGHTING 37.367408 -77.400077
4 37.367619 -77.399897 ... DOWN LIGHTING 37.367635 -77.399944
I’m sure it is something stupid simple but I haven’t been able to track down the issue yet. My guess is my JOIN, but I have read numerous tutorials, been all over W3Schools, tried different JOINS (INNER JOIN, RIGHT JOIN, RIGHT OUTER JOIN, etc) and watched Youtube tutorials. If it is not in my SQL statement then recommendations on table formatting are welcome, if any other information is needed just let me know.
I have no control over the MYSQL version, this is run off a remote server, hosted by SurpassHosting (I do NOT recommend them).
MYSQL Server version: 5.0.95-community
Protocol version: 10
Running SQL query through PHPMyAdmin Version information: 3.4.10.1 and MSQL WorkBench 5.2.37 CE
It seems clear that for the same
loc_idof1that there are 4 records in the left table (d_view_angles) and 3 records in the right table (c_survey_elements).This may mean that you need to use more than just
loc_idto join on.Displaying the relevant contents of each table will make this clear.