Sort the latest registrations according to the registration date

Asked

Viewed 10,006 times

3

Sort the latest ads registered in mysql, with the dates closest to the current date using PHP
Field type (date) in mysql is (date)

This is my query:

SELECT * FROM frame_anuncio WHERE ativo = 'Sim' ORDER BY destaque ASC LIMIT $inicio, $maximo;
  • @NULL No. It’s talking about Mysql, not SQL Server.

  • This question can help you both to search in an orderly way and in a custom format How to sort a Mysql search with date in d-m-Y format?

  • sql am doing so: $sql = mysql_query("SELECT * FROM frame_anuncio WHERE active = 'Yes' ORDER BY highlight ASC LIMIT $start, $maximo")or die(mysql_error();

  • @user3081 You are ordering from minor to major trade the ASC for DESC.

  • @NULL This has nothing to do with it, man. This destaque, at first, has no relation to date.

  • If I exchange the ASC for DESC the ads in highlights are not at the top of the list...

  • The highlight are ads with photos, IE, You have to list first the ads with photos after the rest...and everyone has to be active active

  • what is the name of your date field?

  • The name of the date field is: date and the type is: date

  • @user3081 I edited my answer. See if it helps you. If it helps you, mark it as settled - I’ve been following you around and it seems like it’s not something you’re used to doing. If they help you, help us keep helping you. =)

  • @user3081 until today I have not located where it marks as solved...?

  • @user3081 In the answer, just below the up and down arrows that are located at the top left, there is a "positive" button. Click on it.

  • On the left side of the answer you will see some gray bandstand signs, click on it

Show 9 more comments

1 answer

4


Use the clause ORDER BY in your query for this, this way:

SELECT id FROM table ORDER BY date DESC

Where:

  • id = given to be returned;
  • table = table to be consulted;
  • data = date of registration in the database;
  • DESC = return in a decreasing way, from the largest to the smallest - using the logic of days: bring 30 before 29, just what you want.

Based on the editing of your post, do the following:

$sql = mysql_query("SELECT * FROM frame_anuncio WHERE ativo = 'Sim' ORDER BY destaque ASC, data DESC LIMIT $inicio, $maximo")or die(mysql_error());
  • Thanks @Caesar for editing! I didn’t even know I had placed WHERE in place of ORDER BY.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.