1

我目前有下面的代码,它可以让我获得第二个最小的数字,但我想获得所有第二个最小的数字并将它们链接到他们的名字,而不是其中一个。假设表格中的数字由以下组成:

Name| number
----|------
w     2 
a     8 
s     2
e     2
z     3

我想得到

w 2
s 2
e 2

现在我刚刚得到w 2

SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);
4

3 回答 3

2

如果此代码为您提供第二小的数字(您想要的):

SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table);

然后只需执行以下操作:

select *
from table
where col = (SELECT MAX(col) FROM table WHERE col NOT IN (SELECT MAX(col) FROM table));
于 2013-09-12T18:55:30.207 回答
0

我不太明白,但如果你使用LIMIT 1,1,你只会得到 1 行或没有。只需使用 @491243 对您的问题的评论即可。

SELECT * FROM tablename WHERE number = (SELECT MIN(number) FROM tableName);

算了,现在我明白了这个问题。尝试这个:

SELECT * FROM tablename WHERE number =
    (SELECT number FROM tablename WHERE number != 
        (SELECT MIN(number) FROM tablename) ORDER BY number LIMIT 1);

希望这可以帮助。

编辑:使用 SQLFiddle 表:

SELECT * FROM ships WHERE gunsize = (
    SELECT gunsize FROM ships WHERE gunsize != 
        (SELECT MIN(gunsize) FROM ships) ORDER BY gunsize LIMIT 1);
于 2013-09-12T18:11:31.907 回答
0

http://sqlfiddle.com/#!2/9ca94/11

SELECT name,(SELECT MAX(gunsize) FROM ships s2 
WHERE s2.Name=s.name and gunsize not in (select max(gunsize) from ships))as gunsizes
FROM ships s

像这样的东西?

于 2013-09-12T18:48:35.493 回答