简体   繁体   English

从多个表中选择并左联接到一个表

[英]Select from multiple table and left join to one table

Anybody can help me in solving this problem?? 有人可以帮助我解决这个问题吗?

When im joining only 2 tables. 当我即时加入只有2表。 It work fine. 工作正常。

在此处输入图片说明

but when im trying to select from multiple table. 但是当我试图从多个表中选择。 its not working. 它不起作用。 This is the example of not working code 这是不工作代码的示例

SELECT ca.cdate as tarikh, sum(transaction.amount) as totalamount 
FROM agent ag, town tw, calendar ca 
LEFT JOIN transaction ON ca.cdate =  DATE_FORMAT(transaction.created_at, '%Y-%m-%d') 
WHERE ca.cdate >= '2014-06-01' AND ca.cdate <= '2014-06-11' 
AND transaction.agent_id = ag.id 
AND ag.town_id = tw.id 
AND tw.state_id = 7;

the result is 结果是

在此处输入图片说明

After add the group by 添加组之后

SELECT ca.cdate as tarikh, sum(transaction.amount) as totalamount 
FROM agent ag, town tw, calendar ca 
LEFT JOIN transaction ON ca.cdate =  DATE_FORMAT(transaction.created_at, '%Y-%m-%d') 
WHERE ca.cdate >= '2014-06-01' AND ca.cdate <= '2014-06-11' 
AND transaction.agent_id = ag.id 
AND ag.town_id = tw.id 
AND tw.state_id = 7
GROUP BY
ca.cdate;

在此处输入图片说明

Trying query suggest by Ambrish 尝试由Ambrish查询建议

SELECT ca.cdate as tarikh, sum(transaction.amount) as totalamount 
FROM calendar ca 
LEFT JOIN transaction ON ca.cdate = DATE_FORMAT(transaction.created_at, '%Y-%m-%d') 
LEFT JOIN agent ag on transaction.agent_id = ag.id 
LEFT JOIN town tw on ag.town_id = tw.id 
WHERE ca.cdate >= '2014-06-01' 
AND ca.cdate <= '2014-06-11'
AND tw.state_id = 7;

The result is 结果是 在此处输入图片说明

the 2nd by Ambrish 第二届Ambrish

SELECT ca.cdate as tarikh, sum(transaction.amount) as totalamount 
FROM calendar ca 
LEFT JOIN transaction ON ca.cdate =  DATE_FORMAT(transaction.created_at, '%Y-%m-%d') 
LEFT JOIN agent ag on transaction.agent_id = ag.id
LEFT JOIN town tw on ag.town_id = tw.id 
WHERE ca.cdate >= '2014-06-01' AND ca.cdate <= '2014-06-11' 
AND tw.state_id = 7
group by ca.cdate;

the result is 结果是

在此处输入图片说明

在此处输入图片说明

SELECT ca.cdate as tarikh, sum(transaction.amount) as totalamount 
FROM calendar ca 
LEFT JOIN transaction ON ca.cdate =  DATE_FORMAT(transaction.created_at, '%Y-%m-%d') 
LEFT JOIN agent ag on transaction.agent_id = ag.id
LEFT JOIN town tw ag.town_id = tw.id 
WHERE ca.cdate >= '2014-06-01' AND ca.cdate <= '2014-06-11' 
AND tw.state_id = 7
group by ca.cdate;

If no results are returned then it could be possible that there are no matching records. 如果没有返回结果,则可能没有匹配的记录。 So, try the query without (or subset of) WHERE clause. 因此,请尝试不使用WHERE子句(或其子集)的WHERE Or the query with different values. 或具有不同值的查询。

SELECT
calendar.cdate,
sum(transaction.amount)
FROM
calendar
LEFT JOIN
(
    select transaction.amount, transaction.created_at from transaction, agent, town
    where
    transaction.agent_id = agent.id
    and agent.town_id = town.id
    and town.state_id = 14
)
transaction
ON calendar.cdate = DATE_FORMAT(transaction.created_at, "%Y-%m-%d") 
WHERE
calendar.cdate >= '2014-06-01' and calendar.cdate <= '2014-06-11'
GROUP BY
calendar.cdate;

Finally i manage to find the answer by using sub query..Thank you for your help.. 最后,我设法通过子查询找到答案。谢谢您的帮助。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM