[英]LEFT JOIN not getting desired result
So I have 2 tables, first table is clock_in_out I am selecting loggedin and loggedout dates from this table. 所以我有2个表,第一个表是clock_in_out我正在从该表中选择登录日期和注销日期。 The second table breaks I am selecting in and out fields.
第二个表中断了我要选择的输入和输出字段。 There can be fiedls in clock_in_out that dont match the breaks table... I am joining them on user_id.
在clock_in_out中可能有不匹配breaks表的文件...我在user_id上加入它们。
The problem is I only get results when breaks has a matching field, I thought left join would get all data from the left table and use fake rows with NULL for non matched data. 问题是我只有在中断具有匹配字段的情况下才能获得结果,我认为左联接将从左表中获取所有数据,并对不匹配的数据使用带有NULL的伪行。
Below is my query: 以下是我的查询:
SELECT `clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `breaks`.`in`,
`breaks`.`out` FROM (`clock_in_out`)
LEFT JOIN `breaks` ON `clock_in_out`.`user_id` = `breaks`.`user_id`
WHERE
`clock_in_out`.`user_id` = 47
AND `breaks`.`user_id` = 47
AND `clock_in_out`.`loggedin` LIKE "%2015-03-13%"
AND `clock_in_out`.`loggedout` LIKE "%2015-03-13%"
AND `breaks`.`in` LIKE "%2015-03-13%"
AND `breaks`.`out` LIKE "%2015-03-13%"
I am using CodeIgniter's prepared statements for queries and I am unsure how to get the desired result using them: Here is what I have: 我正在使用CodeIgniter的准备好的查询语句,但不确定如何使用它们来获得所需的结果:这是我所拥有的:
$this->db->select('clock_in_out.loggedin, clock_in_out.loggedout, breaks.in, breaks.out');
$this->db->from('clock_in_out');
$this->db->join('breaks','clock_in_out.user_id = breaks.user_id', 'LEFT');
$this->db->like('breaks.in', $today);
$this->db->like('breaks.out', $today);
$this->db->where('clock_in_out.user_id = '.$user_id);
$this->db->like('clock_in_out.loggedin', $today);
$this->db->like('clock_in_out.loggedout', $today);
$q = $this->db->get();
This produces: 这将产生:
SELECT `clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `breaks`.`in`, `breaks`.`out` FROM (`clock_in_out`)
LEFT JOIN `breaks` ON `clock_in_out`.`user_id` = `breaks`.`user_id`
WHERE `clock_in_out`.`user_id` = 47
AND `breaks`.`in` LIKE '%2015-03-13%'
AND `breaks`.`out` LIKE '%2015-03-13%'
AND `clock_in_out`.`loggedin` LIKE '%2015-03-13%' AND `clock_in_out`.`loggedout` LIKE '%2015-03-13%'
Query produced from CI answer edits: 从CI答案编辑产生的查询:
SELECT `clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `breaks`.`in`, `breaks`.`out` FROM (`clock_in_out`)
LEFT JOIN `breaks` ON `clock_in_out`.`user_id` = `breaks`.`user_id`
WHERE `clock_in_out`.`user_id` = 47
AND `clock_in_out`.`loggedin` LIKE '%2015-03-13%'
AND `clock_in_out`.`loggedout` LIKE '%2015-03-13%'
Regards 问候
You get only the matching fields from breaks table because you are using it inside WHERE
. 您仅从breaks表中获得匹配的字段,因为您正在
WHERE
中使用它。 Try this: 尝试这个:
SELECT `clock_in_out`.`loggedin`, `clock_in_out`.`loggedout`, `breaks`.`in`,
`breaks`.`out` FROM (`clock_in_out`)
LEFT JOIN `breaks` ON
`clock_in_out`.`user_id` = `breaks`.`user_id`
AND `breaks`.`in` LIKE "%2015-03-13%"
AND `breaks`.`out` LIKE "%2015-03-13%"
WHERE
`clock_in_out`.`user_id` = 47
AND `clock_in_out`.`loggedin` LIKE "%2015-03-13%"
AND `clock_in_out`.`loggedout` LIKE "%2015-03-13%"
Make sure, that $today
is properly escaped to avoid SQL injection. 确保
$today
已正确转义以避免SQL注入。
$this->db->select('clock_in_out.loggedin,
clock_in_out.loggedout,
breaks.in,
breaks.out');
$this->db->from('clock_in_out');
$this->db->join('breaks','clock_in_out.user_id = breaks.user_id
AND `breaks`.`in` LIKE "%'.$today.'%"
AND `breaks`.`out` LIKE "%'.$today.'%"', 'LEFT');
$this->db->where('clock_in_out.user_id = '.$user_id);
$this->db->like('clock_in_out.loggedin', $today);
$this->db->like('clock_in_out.loggedout', $today);
$q = $this->db->get()
The LEFT JOIN
does what you say (selects all the rows from the left table, pairs them with rows full of NULL
s when there is no match on the right table). LEFT JOIN
执行您所说的操作(从左表中选择所有行,当右表中没有匹配项时,将它们与充满NULL
的行配对)。
But then the WHERE
conditions come and filter the joined set. 但是随后
WHERE
条件出现并筛选联接的集合。 And your WHERE
conditions on fields of the right-side table ( breaks
) filter out the rows having NULL
values on those fields. 您在右侧表的字段上的
WHERE
条件( breaks
)会过滤掉在这些字段上具有NULL
值的行。
Just remove condition for breaks
. 只需删除
breaks
条件即可。 user_id
= 47 user_id
= 47
SELECT
`clock_in_out`.`loggedin`,
`clock_in_out`.`loggedout`,
`breaks`.`in`,
`breaks`.`out`
FROM (`clock_in_out`)
LEFT JOIN `breaks` ON `clock_in_out`.`user_id` = `breaks`.`user_id`
WHERE
`clock_in_out`.`user_id` = 47
AND `clock_in_out`.`loggedin` LIKE "%2015-03-13%"
AND `clock_in_out`.`loggedout` LIKE "%2015-03-13%"
AND (`breaks`.`in` LIKE "%2015-03-13%" or `breaks`.`in` is null )
AND (`breaks`.`out` LIKE "%2015-03-13%" or `breaks`.`out` is null)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.