0

我希望我的结果按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

4 回答 4

2

一个小错误。尝试ORDER BY代替GROUP BY. ORDER 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
于 2012-09-08T11:33:36.983 回答
1

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

于 2012-09-08T11:33:31.983 回答
0

尝试将其附加到末尾:

ORDER BY
    a.right DESC
于 2012-09-08T11:33:59.060 回答
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
于 2012-09-08T11:34:29.670 回答