Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix SQL syntax near JOIN?

Tags:

sql

mysql

I am fighting with a syntax error and I can't really find the problem.

this is my query

 SELECT * FROM wp_postmeta PM1
  WHERE PM1.meta_key = '_pronamic_google_maps_latitude'
  AND PM1.post_id = '$id'
  JOIN wp_postmenta PM2 
  WHERE PM2.post_id = PM1.post_id 
  AND PM2.meta_key = '_pronamic_google_maps_longitude'

and getting this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'JOIN wp_postmeta PM2' at line 3

could please someone can give me a hint what i'm missing?

like image 351
Side Avatar asked Dec 09 '25 20:12

Side


2 Answers

First have to come the join then the rest of the query. So something like

 SELECT * FROM wp_postmeta PM1
  JOIN wp_postmenta PM2 on PM1.post_id = PM2.post_id
  WHERE PM1.meta_key = '_pronamic_google_maps_latitude'
  AND PM1.post_id = '$id'
  AND PM2.meta_key = '_pronamic_google_maps_longitude' 
                                
like image 180
Daniel Wehner Avatar answered Dec 11 '25 09:12

Daniel Wehner


You've two where clauses and the join order is wrong:

select ...
from ...
join ... on ...
where ... and ...
like image 26
Denis de Bernardy Avatar answered Dec 11 '25 09:12

Denis de Bernardy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!