mysql - How can I select the second minimal value within a inner join? -


i have query

select c.id, c.name, c.email, c.totalpets, min(p.date_created) first_order, min(p.weight) min_weight_bought, max(p.weight) max_weight_bought, count(p.ordernumber) total_orders orders p inner join customers c on p.customer_id = c.id p.approved = 1 , c.totalpets >= 1 group c.id having total_orders > 1 

note first_order gives me first result of row, right? trying customer first order , customer second order. how can within inner join?

thanks

select c1.id,        c1.name,        c1.email,        c1.totalpets,        p1.date_created orders p1 inner join customers c1     on p1.customer_id = c1.id  (     select count(*)     orders p2     inner join customers c2         on p2.customer_id = c2.id     c2.id = c1.id , p2.date_created <= p1.date_created ) <= 2  order c1.id; 

here running demo shows simplified version of above query (and simplified data set) in action:

sqlfiddle


Comments