Problem with ORDER BY: DESC

0

Good, I have the following problem, that I have a social network where people can post things and send messages, the thing is that by doing select of all publications and show them the order by UNIX_TIMESTAMP (data) DESC does not work, I do not order them, the funny thing is that I managed to do it but with another BBDD, which coincidentally I can not find.

BBDD:

CREATE TABLE IF NOT EXISTS 'publicacions' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'contingut' text NOT NULL,
  'usuari' int(11) NOT NULL,
  'imagen' varchar(255) DEFAULT NULL,
  'data' datetime NOT NULL

SQL:

$sql = "select usuaris.nom, usuaris.cognom, publicacions.contingut from usuaris, publicacions where usuaris.id = publicacions.usuari order by UNIX_TIMESTAMP (data) DESC;";
    
asked by Juliian68 14.06.2017 в 15:56
source

2 answers

1

I do not see the need to use UNIX_TIMESTAMP , DATETIME you can order it without problems by DESC , and also what you should use is a INNER JOIN , like this:

SELECT u.nom, u.cognom, p.contingut 
FROM publicacions as p
INNER JOIN usuaris as u
ON (u.id = p.usuari)
ORDER BY p.data DESC;

I hope my answer will be good, greetings.

    
answered by 14.06.2017 в 16:38
0

I would like to see the trace of the error to have a clearer idea of the problem, however in my experience I would do the query of the following form:

Using alias is a good practice for large querys since it helps you keep the query organized.

$sql = "SELECT usuaris.nom, usuaris.cognom, publicacions.contingut FROM usuaris as u, publicacions as p WHERE u.id = p.usuari ORDER BY nombre_tabla.nombre_columa DESC";
    
answered by 14.06.2017 в 17:23