[英]mysql two table with different record in one query
Need to show how many billing_amount has be done by particular user on that target month? 是否需要显示特定用户在该目标月份完成了多少billing_amount? I have two tables, one is target and the other is closure_emp. 我有两个表,一个是目标表,另一个是closure_emp。
SELECT fk_user_id,target_month,target
FROM `target`
WHERE fk_user_id='31' and target_month > DATE_SUB(now(), INTERVAL 6 MONTH)
This shows 6 records 显示6条记录
fk_user_id target_month target
31 2013-08-01 100000
31 2013-09-01 100000
31 2013-10-01 120000
31 2013-11-01 120000
31 2013-12-01 120000
and 和
SELECT *
FROM `closure_employee`
WHERE ce_recruiter_id='31' and offer_date > DATE_SUB(now(), INTERVAL 7 MONTH)
This shows 2 records 显示2条记录
user_id billing_amount offer_date joining_date
31 390000 2013-08-30 2-09-2013
31 208354 2013-09-30 25-11-2013
I need a query to show both the table in one as below 我需要一个查询以将两个表都显示在下面,
fk_user_id target_month target user_id billing_amount
31 2013-08-01 100000 31 390000
31 2013-09-01 100000 31 208354
31 2013-10-01 120000 31 null
31 2013-11-01 120000 31 null
31 2013-12-01 120000 31 null
Please help me. 请帮我。
It seems like you can get the result by using a LEFT JOIN
between your tables. 看起来您可以通过在表之间使用LEFT JOIN
来获得结果。 Since you want to return all rows from the target
table you will then LEFT JOIN
the closure_employee
table on the user_id
. 由于要返回target
表中的所有行,因此将LEFT JOIN
到user_id
的closure_employee
表中。
In order to assign the correct billing_amount
to each row from target
, you will need to use the month()
value for the target_month
and the offer_date
- This will also be part of your join condition. 为了分配正确billing_amount
从各行target
,你将需要使用month()
值的target_month
和offer_date
-这也将是你的连接条件的一部分。
The query will be similar to the following: 该查询将类似于以下内容:
select t.fk_user_id,
t.target_month,
t.target,
e.user_id,
e.billing_amount
from target t
left join closure_employee e
on t.fk_user_id = e.user_id
and month(t.target_month) = month(e.offer_date)
and e.offer_date > DATE_SUB(now(), INTERVAL 7 MONTH)
where t.target_month > DATE_SUB(now(), INTERVAL 6 MONTH);
See SQL Fiddle with Demo . 请参阅带有演示的SQL Fiddle 。 You'll notice that the offer_date
filter to return the previous 7 months, has been added to the join condition instead of the where clause - doing this will still return the rows from the closure_employee
in that timeframe, but it will also return all rows from your target
table which appears to be the behavior that you want. 您会注意到,将要返回前7个月的offer_date
过滤器已添加到offer_date
条件中,而不是where子句中-这样做仍将返回该时间范围内closure_employee
的行,但还会返回您的target
表似乎是您想要的行为。
If you need to add the filter by the ce_recruiter_id
and the fk_user_id
then you would alter the above query to: 如果需要通过ce_recruiter_id
和fk_user_id
添加过滤器,则可以将上述查询更改为:
select t.fk_user_id,
t.target_month,
t.target,
e.user_id,
e.billing_amount
from target t
left join closure_employee e
on t.fk_user_id = e.user_id
and month(t.target_month) = month(e.offer_date)
and e.offer_date > DATE_SUB(now(), INTERVAL 7 MONTH)
and e.ce_recruiter_id='31'
where t.target_month > DATE_SUB(now(), INTERVAL 6 MONTH)
and t.fk_user_id='31'
SELECT
t1.fk_user_id, t1.target_month, t1.target, t2.billing_amount
FROM
target AS t1, closure_employee AS t2
WHERE
t1.fk_user_id = 31 AND t2.user_id = 31
AND
t1.target_month > DATE_SUB(now(), INTERVAL 6 MONTH);
Hope that helps (and works - is off top of my head! 希望能帮上忙(而且行之有效-不在我头上!
This is assuming that fk_user_id = user_id for your query 假设您的查询的fk_user_id = user_id
You should also check out how to use JOIN on a table - 您还应该查看如何在表上使用JOIN-
http://dev.mysql.com/doc/refman/5.0/en/join.html http://dev.mysql.com/doc/refman/5.0/en/join.html
however you might want to start with a simpler example to get your head around it! 但是,您可能想从一个更简单的示例入手!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.