Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax Error when Inner Joining two tables

Tags:

mysql

I'm trying to join tables events and venues, however I ran into an issue whilst inner joining. This is how I did it:

$query =    "SELECT e.*, v.* FROM events e WHERE start_datetime >= '$DATE_START_SELECTED' AND end_datetime < '$DATE_END_SELECTED'".
           "INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID";

And for some reason I have an error like this:

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 'INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID' at line 1

Any idea why? Thanks!


Edit:

I updated the code to look like this:

$query =    "SELECT e.*, v.* FROM events e ".
        "INNER JOIN venues v ON e.VENUE_LOCATION = v.VENUE_ID".
        "WHERE start_datetime >= '$DATE_START_SELECTED' AND end_datetime < '$DATE_END_SELECTED'";

Only to get 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 'start_datetime >= '2012-03-15 06:00:00' AND end_datetime < '2012-03-16 05:59:00'' at line 1

like image 531
pufAmuf Avatar asked Dec 12 '25 01:12

pufAmuf


1 Answers

Your INNER JOIN needs to come before the WHERE clause.

E.g.,

select e.*, v.*
from events e
inner join venues v on e.VENUE_LOCATION = v.VENUE_ID
where start_datetime >= '$DATE_START_SELECTED' 
    and end_datetime < '$DATE_END_SELECTED'
like image 154
D'Arcy Rittich Avatar answered Dec 17 '25 00:12

D'Arcy Rittich



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!