Mysql query to match and join two rows for a given date. Hi I am newbie, have googled and searched stack overflow for a days but I can't find the answer. Please help with the following problem
I have a table: people
Id Leader Name1 Name2 StartDate EndDate
123 1 Person1 2013-02-11 2013-02-17
123 0 Person2 2013-02-13 2013-02-13
123 0 Person3 2013-02-13 2013-02-13
I have a query1
(SELECT t1.Id, t1.Name1, t1.Name2
FROM `dbo`.`people` t1
WHERE t1.StartDate >= '2013-02-11'
AND t1.Leader = 1)
UNION
(SELECT t2.Id, t2.Name1, t1.Name2
FROM `dbo`.`people` t2
WHERE t2.StartDate >= '2013-02-11'
AND t2.Leader = 0)
This returns
Id Name1 Name2
123, Person1
I have a query2
(SELECT t1.Id, t1.Name1, t1.Name2
FROM `dbo`.`people` t1
WHERE t1.StartDate >= '2013-02-13'
AND t1.Leader = 1)
UNION
(SELECT t2.Id, t2.Name1, t1.Name2
FROM `dbo`.`people` t2
WHERE t2.StartDate >= '2013-02-13'
AND t2.Leader = 0)
This returns
Id Name1 Name2
123, Person1
123, Person2
123, Person3
I need the result for 2012-02-11 to show
Id Name1 Name2
123, Person1 null
and for the 2012-02-13 to show
Id Name1 Name2
123, Person1 Person2
123, Person1 Person3