Home  >  Q&A  >  body text

mysql - 论坛帖子排序问题

天蓬老师天蓬老师2743 days ago724

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 16:09:11

    It is recommended to use a temporary table to sort again and again. Each time the sorting results are placed in the temporary table to see if they are correct, and then the next sorting is performed.

    Finally, optimize all sorted SQL statements into one.

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 16:09:11

    PS: SQL should be posted in code form as much as possible for the respondent to modify

    You can left join the main table to a table with only the first three records, similar to:

    SELECT a.*,
           b.count AS new_count
    FROM test a
    LEFT JOIN
      ( SELECT * FROM test ORDER BY count DESC LIMIT 0, 3 ) b
    ON a.id=b.id
    ORDER BY new_count DESC,date DESC;

    reply
    0
  • Cancelreply