mySQL ORDER BY temporary calculated column

546 views Asked by At

First you must know: I have two tables "USERS" and "SONGS"

How is possible to check some conditions and place the result on a temp column? For example foreach song in result of select must be calculate the bool and add it to temp column so I can do other calculations then and make it the primary order......

$USER = SELECT * FROM USERS WHERE SOMECONDITION

SELECT * FROM SONGS
------
(
IF SONGS.LANG  LIKE $USER[LANG]  THEN TEMPCOLUMN.ADD "1" ELSE TEMPCOLUMN.ADD 0
IF SONGS.GENR1 LIKE $USER[GENR1] THEN TEMPCOLUMN.ADD "1" ELSE TEMPCOLUMN.ADD 0
IF SONGS.GENR2 LIKE $USER[GENR2] THEN TEMPCOLUMN.ADD "1" ELSE TEMPCOLUMN.ADD 0
)
------
ORDER BY TEMPCOLUMN DESC
1

There are 1 answers

4
Jimmy T. On BEST ANSWER

This should do the same thing:

SELECT * FROM SONGS
ORDER BY
  SONGS.LANG LIKE $USER[LANG] DESC,
  IF SONGS.GENR1 LIKE $USER[GENR1] DESC,
  IF SONGS.GENR2 LIKE $USER[GENR2] DESC;