Multiple column in order by clause - mysql

你。 提交于 2019-12-11 08:56:40

问题


I have two columns in a table say, LIKE and FAVORITES (int value)

See the chart:

╔════╦══════╦══════════╗
║ ID ║ LIKE ║ FAVORITE ║
╠════╬══════╬══════════╣
║  1 ║   25 ║        9 ║
║  2 ║    5 ║       17 ║
║  3 ║    6 ║        1 ║
║  4 ║   45 ║        0 ║
║  5 ║    3 ║       44 ║
╚════╩══════╩══════════╝

Now, I want to select the Maximum Like and Favorites IDs from the SELECT clause. I have tried

SELECT ID from TABLE WHERE CONDITION ORDER BY LIKE,FAVORITES DESC

But the result shows the rows based on LIKE DESC order.

The result should be

╔════╗
║ ID ║
╠════╣
║  5 ║
║  4 ║
║  1 ║
║  2 ║
║  3 ║
╚════╝

回答1:


I think you need to add those two columns. eg,

SELECT ID
FROM tableName
ORDER BY `LIKE` + FAVORITE DESC
  • SQLFiddle Demo

Result:

╔════╗
║ ID ║
╠════╣
║  5 ║
║  4 ║
║  1 ║
║  2 ║
║  3 ║
╚════╝


来源:https://stackoverflow.com/questions/15131682/multiple-column-in-order-by-clause-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!