Reputation: 289
I have a breaks and clock_in_out table and I want to get the all values from both and join them on their user_id column. But I am getting incorrect synatx to use near JOIN ON, not a clue why.. Here is my query
SELECT *
FROM `breaks`
WHERE `user_id` = 1
AND `in_date` LIKE '%2015-03-03%'
AND `out_date` LIKE '%2015-03-03%'
JOIN `clock_in_out` ON `breaks`.`user_id` = `clock_in_out`.`user_id`
WHERE `loggedin_date`
LIKE '%2015-03-03%'
AND `loggedout_date` LIKE '%2015-03-03%'
Upvotes: 0
Views: 42
Reputation: 619
Correct syntax for the this is as follows
SELECT *
FROM `breaks`
JOIN `clock_in_out` ON `breaks`.`user_id` = `clock_in_out`.`user_id`
WHERE `user_id` = 1
AND `in_date` LIKE '%2015-03-03%'
AND `out_date` LIKE '%2015-03-03%'
AND `loggedin_date` LIKE '%2015-03-03%'
AND `loggedout_date` LIKE '%2015-03-03%
Upvotes: 0
Reputation: 69440
Your syntax is wrong. The correct syntax for join is Select ... from .. join ... on ... where
SELECT *
FROM `breaks`
JOIN `clock_in_out` ON `breaks`.`user_id` = `clock_in_out`.`user_id`
WHERE `loggedin_date`
LIKE '%2015-03-03%'
AND `loggedout_date` LIKE '%2015-03-03%'
AND `user_id` = 1
AND `in_date` LIKE '%2015-03-03%'
AND `out_date` LIKE '%2015-03-03%'
Upvotes: 1
Reputation: 780909
You can't mix WHERE
and JOIN
clauses like that. The order is: SELECT
, FROM
, JOIN
, WHERE
, GROUP BY
, HAVING
, LIMIT
. So it should be:
SELECT *
FROM `breaks`
JOIN `clock_in_out` ON `breaks`.`user_id` = `clock_in_out`.`user_id`
WHERE `user_id` = 1
AND `in_date` LIKE '%2015-03-03%'
AND `out_date` LIKE '%2015-03-03%'
AND `loggedin_date` LIKE '%2015-03-03%'
AND `loggedout_date` LIKE '%2015-03-03%'
See the MySQL documentation for the complete syntax.
Upvotes: 1
Reputation: 18737
WHERE
clause should be after JOIN
:
SELECT *
FROM `breaks` JOIN
`clock_in_out` ON `breaks`.`user_id` = `clock_in_out`.`user_id`
WHERE `user_id` = 1
AND `in_date` LIKE '%2015-03-03%'
AND `out_date` LIKE '%2015-03-03%'
AND `loggedin_date` LIKE '%2015-03-03%'
AND `loggedout_date` LIKE '%2015-03-03%'
Upvotes: 2