0

这是一个非常奇怪的问题。我有 3 个表,所有表都与 agent_id 上的关键关系。

现在这个查询:

select DATE_FORMAT(created_on,"%d/%m/%y") as date
from logs
where agent_id = "18"
and log_status="1"
and date_format(created_on,"%m/%y") = "12/12"
group by YEAR(created_on), MONTH(created_on), DAY(created_on) desc

完全按照它应该做的,它返回 3 行:

date
31/12/12
30/12/12
26/12/12

但是,当我加入 2 个其他表时,它会排除31/12/12. 这是查询:

select  DATE_FORMAT(l.created_on,"%d/%m/%y") as date
from logs l
join logs_rewards lr on l.id = lr.related_log_id 
join agents a on a.id = l.agent_id 
where l.log_status = "1" 
and DATE_FORMAT(l.created_on,"%m/%y") = '12/12'
AND l.agent_id = '18' 
group by YEAR(l.created_on), MONTH(l.created_on), DAY(l.created_on) desc

这返回:

date
30/12/12
26/12/12

我不知道为什么会这样。有什么帮助吗?

我试过改变AND l.agent_id = '18'AND a.id = '18'结果相同。

4

2 回答 2

0

也许您想用LEFT JOIN替换JOIN以从日志中获取所有结果。

于 2012-12-31T09:46:15.643 回答
0

这是因为数据并未映射到所有 3 个表中。

下面的查询将起作用

select  DATE_FORMAT(l.created_on,"%d/%m/%y") as date
from logs l
left join logs_rewards lr on l.id = lr.related_log_id 
left join agents a on a.id = l.agent_id 
where l.log_status = "1" 
and DATE_FORMAT(l.created_on,"%m/%y") = '12/12'
AND l.agent_id = '18' 
group by YEAR(l.created_on), MONTH(l.created_on), DAY(l.created_on) desc
于 2012-12-31T09:49:41.953 回答