Ajit Singh
Ajit Singh

Reputation: 1150

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? I have two tables, one is target and the other is 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

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

  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.

Upvotes: 0

Views: 95

Answers (2)

Taryn
Taryn

Reputation: 247680

It seems like you can get the result by using a LEFT JOIN between your tables. Since you want to return all rows from the target table you will then LEFT JOIN the closure_employee table on the user_id.

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.

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. 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.

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:

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'

Upvotes: 2

GrahamTheDev
GrahamTheDev

Reputation: 24825

 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

You should also check out how to use JOIN on a table -

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!

Upvotes: -1

Related Questions