我有3个表用户,休息,clock_in_out如下所示:
用户:
符:
Clock_in_out表:
我需要的查询类似于以下内容:
SELECT `clock_in_out`.`created`, `breaks`.`in`, `breaks`.`out`, `clock_in_out`.`user_id`, `users`.`id`,
`clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `users`.`firstname`, `users`.`lastname`
FROM (`clock_in_out`)
JOIN `users` ON `users`.`id` = `clock_in_out`.`user_id`
JOIN `breaks` ON `users`.`id` = `breaks`.`user_id`
WHERE `loggedin` LIKE "%2015-03-18%"
AND `clock_in_out`.`user_id` = '88'
ORDER BY `created` ASC
返回:
上面的结果正是我需要的,除非在clock_in_out中有1行,在break中有2行,它会在clock_in_out的loggedin和loggedout字段中添加重复值(我希望这些为NULL)
答案 0 :(得分:0)
LEFT JOIN应该有效
SELECT `clock_in_out`.`created`, `breaks`.`in`, `breaks`.`out`,`clock_in_out`.`user_id`, `users`.`id`,
`clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `users`.`firstname`, `users`.`lastname`
FROM (`clock_in_out`)
LEFT JOIN `users` ON `users`.`id` = `clock_in_out`.`user_id`
LEFT JOIN `breaks` ON `users`.`id` = `breaks`.`user_id`
WHERE `loggedin` LIKE "%2015-03-18%"
AND `clock_in_out`.`user_id` = '88'
ORDER BY `created` ASC
如果没有尝试RIGHT JOIN
答案 1 :(得分:0)
您的查询返回2个结果,因为您的中断表包含用户88的两行。
Sql从clock_in_out返回1行 通过连接用户,它使用用户信息完成此行 但是在连接中断时,它为用户88检索2行。
因为你有2个结果