替换为ORDER BY子查询

时间:2015-04-07 08:01:33

标签: mysql subquery sql-order-by

例如,在http://www.w3schools.com/sql/trysql.asp?filename=trysql_select_orderby2的数据库中(单击“运行SQL”),我想列出在美国所有客户列表中首先拥有大于80的最大CustomerID的客户。所以我用

SELECT * FROM Customers
WHERE Country = 'USA'
ORDER BY CustomerID = (SELECT MAX(CustomerID) FROM Customers 
    WHERE CustomerID > 80 AND Country = 'USA') DESC, PostalCode;

但这不是我正在使用的真实查询。如果查询的SELECT ... FROM ... WHERE ...部分更复杂,那么什么是更优雅的查询?

我想要修改的实际查询是

SELECT post.postid, post.visible, post.userid, post.parentid, post.vote_count
FROM " . TABLE_PREFIX . "post AS post
WHERE post.threadid = $threadid
AND post.visible IN (1" . (!empty($deljoin) ? ", 2" : "") . 
    ($show['approvepost'] ? ", 0" : "") . ")
ORDER BY post.postid = {$threadinfo['firstpostid']} DESC, post.vote_count > 5 DESC, 
    post.dateline $postorder

我试图替换的post.vote_count > 5 DESC部分只有最大的post.vote_count大于5.所以我使用:

SELECT post.postid, post.visible, post.userid, post.parentid, post.vote_count
FROM " . TABLE_PREFIX . "post AS post
WHERE post.threadid = $threadid
AND post.visible IN (1" . (!empty($deljoin) ? ", 2" : "") . 
    ($show['approvepost'] ? ", 0" : "") . ")
ORDER BY post.postid = {$threadinfo['firstpostid']} DESC, post.vote_count = (
    SELECT MAX(post.vote_count)
    FROM " . TABLE_PREFIX . "post AS post
    WHERE post.threadid = $threadid
    AND post.visible IN (1" . (!empty($deljoin) ? ", 2" : "") . 
        ($show['approvepost'] ? ", 0" : "") . ")
    AND post.vote_count > 5
)
DESC, post.dateline $postorder

一切都很好。但是你可以想象一个更复杂的查询,也许是INNER JOIN,它的SELECT ... FROM ... WHERE ...等等都必须在子查询中重复。

所以我的问题是,我怀疑,您是否可以订购查询结果,因此第一项(在这些结果中)具有最大值的字段,其余的则以其他方式排序,而基本上不重写整个在子查询中查询?

2 个答案:

答案 0 :(得分:1)

MySQL不支持CTE,这可能是简化查询的完美解决方案。它们可以通过视图模拟:

CREATE VIEW c AS (SELECT Customer.* FROM Customer WHERE Country = "USA");
SELECT c.* FROM c ORDER BY CustomerID = (SELECT MAX(CustomerID) FROM c) DESC;
DROP VIEW c;

在你的情况下,这将给出:

CREATE VIEW p AS (
    SELECT post.postid, post.visible, post.userid, post.parentid, post.vote_count
    FROM " . TABLE_PREFIX . "post AS post
    WHERE post.threadid = $threadid
    AND post.visible IN (1" . (!empty($deljoin) ? ", 2" : "") . 
        ($show['approvepost'] ? ", 0" : "") . ")
);
SELECT p.* FROM p
ORDER BY p.postid = {$threadinfo['firstpostid']} DESC, p.vote_count = (
    SELECT MAX(p.vote_count)
    FROM p
    WHERE p.vote_count > 5
)
DESC, post.dateline $postorder;
DROP VIEW p;

答案 1 :(得分:-2)

此查询将按CustomerID

的降序列出数据
 SELECT * FROM Customers
    WHERE Country = 'USA'
    ORDER BY CustomerID  DESC, PostalCode;