mySQL ORDER BY临时计算列

时间:2014-11-11 18:15:11

标签: mysql sql foreach temp

首先你必须知道: 我有两张桌子“USERS”和“SONGS”

如何检查某些条件并将结果放在临时列上? 例如,选择结果中的foreach歌曲必须计算bool并将其添加到temp列,这样我就可以进行其他计算然后使其成为主要的顺序......

$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 个答案:

答案 0 :(得分:1)

这应该做同样的事情:

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;