2015-03-03 103 views
0

我有一個休息和clock_in_out表,我想從兩者獲取所有值,並將其加入到他們的user_id列中。但我越來越不正確synatx使用JOIN附近ON,不知道爲什麼。這裏是我的查詢什麼是這個MySQL加入查詢

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%' 
+1

where子句應該是你的加入 – Ghost 2015-03-03 09:40:12

+0

不太熟悉MySQL後,但你可以有2'WHERE'條款 - ? – CompanyDroneFromSector7G 2015-03-03 10:00:25

回答

1

您不能混用WHEREJOIN這樣的子句。訂單是:SELECT,FROM,JOIN,WHERE,GROUP BY,HAVING,LIMIT。因此它應該是:

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%' 

有關完整的語法,請參閱MySQL documentation

2

WHERE條款應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%' 
1

您的語法錯誤。對於加入正確的語法是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%' 
0

對於這個正確的語法如下

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% 
+0

@Richard歡迎:) – 2015-03-03 10:00:01

+0

謝謝你們!但是,當應該有兩組結果時,返回1個結果行 – Richard 2015-03-03 10:03:58