订购多个领域?

时间:2013-12-10 11:03:44

标签: mysql sql postgresql

我有这个查询

SELECT * FROM
(SELECT u.id,u.email,u.verified,u.verified_on,u.created_on,ca.html AS age,cg.html AS gender,cs.html AS state
FROM users u
LEFT JOIN combo ca ON ca.combo_group='age' AND ca.value =u.age
LEFT JOIN combo cg ON cg.combo_group='gender' AND cg.value =u.gender
LEFT JOIN combo cs ON cs.combo_group='state' AND cs.value =u.state ORDER BY created_on DESC) users ORDER BY email ASC;

我正在使用子查询,因为我想要排序created_on DESC和电子邮件ASC

如果我使用子查询,

会影响性能。

1 个答案:

答案 0 :(得分:5)

为什么不按两个字段订购?

SELECT 
     u.id
    ,u.email
    ,u.verified
    ,u.verified_on
    ,u.created_on
    ,ca.html AS age
    ,cg.html AS gender
    ,cs.html AS state
FROM users u
    LEFT JOIN combo ca ON ca.combo_group='age' AND ca.value =u.age
    LEFT JOIN combo cg ON cg.combo_group='gender' AND cg.value =u.gender
    LEFT JOIN combo cs ON cs.combo_group='state' AND cs.value =u.state 
ORDER BY created_on DESC, email ASC