0

目前要在我的查询中显示最高值,我正在使用:

ORDER BY Height DESC
) T
WHERE RowNum = 1

这将显示最高值,例如 10,但是如果两个条目的值都相同,该怎么办 10。

我怎样才能使它显示两个联合最高值?

4

4 回答 4

2

在 Oracle 中使用,使用排名分析

select a, b
  from (select a, b, rank() over (order by height desc) rnk
          from your_table)
 where rnk = 1;
于 2012-12-13T13:23:47.990 回答
1

一种解决方案是使用 CTE

使用 CTE

;WITH q AS (
  SELECT MAX(Height) AS maxHeight
  FROM   YourTable
)
SELECT *
FROM   YourTable
       INNER JOIN q ON q.maxHeight = Yourtable.Height

或带有子选择的普通 where 子句

SELECT *
FROM   YourTable
WHERE  Height = (SELECT MAX(Height) FROM YourTable)           

就个人而言,我更喜欢使用 CTE,因为它不会使语句混乱(好吧,在这个简单的情况下不是这样,但在实践中,我经常发现它比子选择更具可读性)

于 2012-12-13T13:24:30.347 回答
0

在这里你没有子查询,它更复杂但创建起来很有趣。

select ifnull(@d,@d:=actor_id),actor_id,if(@d>actor_id,1,0)as t 
from t1 having t=0 order by actor_id desc;

这是我电脑中的示例。

mysql> desc t1;
+-------------+----------------------+------+-----+---------------------+-------+
| Field       | Type                 | Null | Key | Default             | Extra |
+-------------+----------------------+------+-----+---------------------+-------+
| actor_id    | smallint(5) unsigned | NO   |     | 0                   |       |
| first_name  | varchar(45)          | NO   |     | NULL                |       |
| last_name   | varchar(45)          | NO   |     | NULL                |       |
| last_update | timestamp            | NO   |     | 0000-00-00 00:00:00 |       |
+-------------+----------------------+------+-----+---------------------+-------+
4 rows in set (0.00 sec)

mysql> select * From t1 order by actor_id desc limit 5;
+----------+------------+-----------+---------------------+
| actor_id | first_name | last_name | last_update         |
+----------+------------+-----------+---------------------+
|    20001 | test       | test      | 2012-12-13 09:12:50 |
|    20001 | test       | test      | 2012-12-13 09:12:51 |
|     2000 | b          | a         | 0000-00-00 00:00:00 |
|     2000 | b          | a         | 0000-00-00 00:00:00 |
|     2000 | b          | a         | 0000-00-00 00:00:00 |
+----------+------------+-----------+---------------------+
5 rows in set (0.00 sec)

mysql>     select ifnull(@d,@d:=actor_id),actor_id,if(@d>actor_id,1,0)as t 
    ->     from t1 having t=0 order by actor_id desc;
+-------------------------+----------+---+
| ifnull(@d,@d:=actor_id) | actor_id | t |
+-------------------------+----------+---+
|                   20001 |    20001 | 0 |
|                   20001 |    20001 | 0 |
+-------------------------+----------+---+
2 rows in set (0.00 sec)
于 2012-12-13T14:06:03.550 回答
0

这样就可以了,而且也很简单,

Select height from table where height=(select max(height) from table)
于 2012-12-13T13:36:46.893 回答