I have one table I want to find first and last record that satisfy criteria of particular month.
SELECT (SELECT column FROM table WHERE [condition] ORDER BY column LIMIT 1) as 'first', (SELECT column FROM table WHERE [condition] ORDER BY column DESC LIMIT 1) as 'last'
This worked for me when I needed to select first and the last date in the event series.
First and last make sense only when you have the output of the query sorted on a field(s).
To get the first record:
select col1 from tab1 order by col1 asc limit 1;
To get the last record:
select col1 from tab1 order by col1 desc limit 1;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With