按降序排序结果

时间:2012-09-08 11:32:31

标签: mysql sql sorting

我希望我的结果按a.right排序(它可能有2个值:1 - true,0 - false)。 a.right=1必须先行的行。试过GROUP BY。但没有帮助。如何实现上述结果?

SELECT
    u.fullname,
    a.id,
    a.content,
    a.addDT,
    a.`right`,
    acr.score,
    acr.checkDT
FROM
    answers a
INNER JOIN users u ON u.id = a.uid
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    a.qid = 1
GROUP BY
    a.`right` DESC

4 个答案:

答案 0 :(得分:2)

有点小错误。请尝试ORDER BY而不是GROUP BYORDER BY用于排序记录,而GROUP BY用于聚合记录。

您的查询应该是

SELECT
    u.fullname,
    a.id,
    a.content,
    a.addDT,
    a.`right`,
    acr.score,
    acr.checkDT
FROM
    answers a
INNER JOIN users u ON u.id = a.uid
LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
WHERE
    a.qid = 1
ORDER BY
    a.`right` DESC

答案 1 :(得分:1)

您应该使用ORDER BY来排序结果。 GROUP BY用于汇总结果。

答案 2 :(得分:0)

尝试将其添加到最后:

ORDER BY
    a.right DESC

答案 3 :(得分:0)

对于排序,您应该使用ORDER BY:

    SELECT
        u.fullname,
        a.id,
        a.content,
        a.addDT,
        a.`right`,
        acr.score,
        acr.checkDT
    FROM
        answers a
    INNER JOIN users u ON u.id = a.uid
    LEFT JOIN `answer_chk_results` acr ON acr.aid = a.id
    WHERE
        a.qid = 1
    ORDER BY
        a.`right` DESC