Jump to content

Selecting the first 8 from a table


LittleNicky

Recommended Posts

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

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

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 rows
SELECT top 8 * FROM table_name ORDER BY date_field DESC

Link to comment
Share on other sites

  • 5 weeks later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...