So I have a table which is structured in the following format:
in_click first_name create_date
100 joe 2011-10-01 10:01
100 joe 2011-10-01 10:05
100 joe 2011-10-01 10:07
100 joe 2011-10-01 10:08
100 joe 2011-10-01 10:10
101 sara 2011-10-01 10:15
101 sara 2011-10-01 10:17
101 sara 2011-10-01 10:20
101 sara 2011-10-01 10:22
For each first name, I want to select the first and last rows.
So it should be the first date with the name joe and the last date
where the first name is joe. Kinda like an if-else statement, but
I’m just not sure how to get that information in MySQL.
Help?!
I’m running the following query to just get everything:
SELECT t.in_click_id, t.keyword, c.lead_id, c.first_name, c.last_name,
ls.create_date, ls.discriminator, l.affiliate_id
FROM lead_status AS ls
INNER JOIN leads AS l ON l.id = ls.lead_id
INNER JOIN tracker AS t ON l.in_click_id = t.in_click_id
INNER JOIN contacts AS c ON ls.lead_id = c.lead_id
WHERE l.affiliate_id NOT IN('1002','1003')
AND ls.create_date BETWEEN '2011-11-09' AND '2011-11-10';
This is what I’m trying to get:
in_click first_name create_date
100 joe 2011-10-01 10:01
100 joe 2011-10-01 10:10
101 sara 2011-10-01 10:15
101 sara 2011-10-01 10:22
This works with the data you provided, can’t say it would work in MySQL, but it works in SQL Server.