简体   繁体   English

MySQL ORDER BY(DESC | ASC)得到相同的结果

[英]MySQL ORDER BY (DESC|ASC) gets the same result

In order to update a table of a tournament in HTML I do the following query: 为了更新HTML中的锦标赛表,请执行以下查询:

SELECT p.pname, 
       team, 
       won, 
       tie, 
       lost, 
       goals, 
       goalsa 
FROM   player p, 
       stats s 
WHERE  p.tid = s.tid 
       AND p.pname = s.pname 
       AND p.tid = 23 
       AND p.groupno = 'A' 
ORDER  BY s.won, 
          s.tie, 
          ( s.goals - s.goalsa ), 
          s.goals, 
          p.pname DESC 

Where as the table player has the form 哪里有桌上型玩家

player (PName, TID, DraftNo, groupNo, team)

and the table stats the form 表格统计表格

stats(won, tie, lost, goals, goalsA, PName, TID)

Currently, in stats there are following entries relevant for the query 当前,在统计信息中有与查询相关的以下条目

0    0    0    0    0    a    23
0    0    0    0    0    c    23
0    0    0    0    0    e    23
1    0    0    2    1    g    23
0    0    1    1    2    i    23

which means that player g won against player i 2-1. 这意味着玩家g击败了玩家i 2-1。

Now, no matter if I specify the ORDER BY option to DESC or ASC , I'm getting the following result: 现在,无论我是否为DESCASC指定ORDER BY选项,我都会得到以下结果:

i    Türkei    0    0     1     1     2
(...) 
g    Italien   1    0     0     2     1

which is exactly the opposite of what required. 这与所要求的完全相反。 Why is this so? 为什么会这样呢? Is there an error in my query? 查询中有错误吗? Obviously I want to order the stats from the player with most points to players with less. 显然,我想从得分最多的玩家到得分更少的玩家订购统计数据。

The ASC or DESC keyword applies only to the column it is placed beside in the ORDER BY , so you are doing a descending order by p.pname rather than by goals. ASCDESC关键字仅适用于在ORDER BY放置在其旁边的列,因此您按p.pname而不是按目标进行降序排列。 You need to apply the DESC to goals and all other columns which should be sorted higher ( won ) for example, if you plan to do an aggregate like SUM(won) . 例如,如果您计划进行类似SUM(won)的聚合,则需要将DESC应用于goals和所有其他列应排在较高( won )的列。

ORDER  BY s.won, 
      s.tie, 
      ( s.goals - s.goalsa ), 
      s.goals DESC, 
      p.pname 

A note about your joining method... You are using an implicit inner join, by supplying a comma-separated list in the FROM clause and the condition in the WHERE clause 关于连接方法的说明...您正在使用隐式内部连接,方法是在FROM子句中提供逗号分隔的列表,并在WHERE子句中提供条件

FROM   player p, 
       stats s 
WHERE  p.tid = s.tid 

It is recommended to use an explicit INNER JOIN instead. 建议改用显式的INNER JOIN The syntax is more modern and often considered easier to read. 语法更现代,通常被认为更易于阅读。 The two are equivalent functionally and in performance, however. 但是,两者在功能和性能上都是等效的。

FROM  player p
      INNER JOIN stats s ON p.tid = s.tid

您需要在排序后的每个字段后加上DESC或ASC:

ORDER BY s.won DESC, s.tie DESC, ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM