LittleNicky Posted August 14, 2006 Share Posted August 14, 2006 Ok so what I want to do is show 8 rows from a table, the 8 rows will be chosen by the most recent dates. How do I do this? Link to comment Share on other sites More sharing options...
murfitUK Posted August 14, 2006 Share Posted August 14, 2006 Well, the first step is to select which fields you want (or use * for all of them). Your sql so far is:SELECT field1, field2,etc FROM table_nameORSELECT * FROM table_nameThen you have to make sure it is sorted correctly by using the order by command and then ASC or DESC for ascending or descending. As you want most recent first I think you should try DESC:SELECT * FROM table_name ORDER BY date_field DESCThen you want to limit the results to the first 8 rows so the LIMIT keyword seems a good one to use:SELECT * FROM table_name ORDER BY date_field DESC LIMIT 0,7This returns all rows between 0 and 7 (ie 8 rows). Don't forget that your first row is 0 (not 1). Link to comment Share on other sites More sharing options...
LittleNicky Posted August 14, 2006 Author Share Posted August 14, 2006 Well, the first step is to select which fields you want (or use * for all of them). Your sql so far is:SELECT field1, field2,etc FROM table_nameORSELECT * FROM table_nameThen you have to make sure it is sorted correctly by using the order by command and then ASC or DESC for ascending or descending. As you want most recent first I think you should try DESC:SELECT * FROM table_name ORDER BY date_field DESCThen you want to limit the results to the first 8 rows so the LIMIT keyword seems a good one to use:SELECT * FROM table_name ORDER BY date_field DESC LIMIT 0,7This returns all rows between 0 and 7 (ie 8 rows). Don't forget that your first row is 0 (not 1).Exactly what I wanted, cheers! Link to comment Share on other sites More sharing options...
nomore_sr Posted August 20, 2006 Share Posted August 20, 2006 Well, the first step is to select which fields you want (or use * for all of them). Your sql so far is:SELECT field1, field2,etc FROM table_nameORSELECT * FROM table_nameThen you have to make sure it is sorted correctly by using the order by command and then ASC or DESC for ascending or descending. As you want most recent first I think you should try DESC:SELECT * FROM table_name ORDER BY date_field DESCThen you want to limit the results to the first 8 rows so the LIMIT keyword seems a good one to use:SELECT * FROM table_name ORDER BY date_field DESC LIMIT 0,7This returns all rows between 0 and 7 (ie 8 rows). Don't forget that your first row is 0 (not 1). this's useful. Thank youAnother way to select the first 8 rowsSELECT top 8 * FROM table_name ORDER BY date_field DESC Link to comment Share on other sites More sharing options...
jhecht Posted August 20, 2006 Share Posted August 20, 2006 SELECT * FROM `table` ORDER BY `column` LIMIT 8That works well for me... Link to comment Share on other sites More sharing options...
Raj Ali Posted September 20, 2006 Share Posted September 20, 2006 Exactly what I wanted, cheers!if u want to chose top 8 rows which are recently entered then u useselect top8<column_name>from <table_name>or if u want randomly rows from table then u useselect*from <table_name> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now