从子查询函数返回2个值

时间:2014-03-03 15:10:05

标签: mysql sql

SELECT DISTINCT 
    utover.startnr, utover.navn
FROM 
    utover, deltakelse
WHERE 
    utover.startnr = (SELECT utover
                      FROM deltakelse
                      GROUP BY utover
                      HAVING COUNT(*) = (SELECT COUNT(*)
                                         FROM deltakelse
                                         GROUP BY utover
                                         ORDER BY COUNT(*) DESC 
                                         LIMIT 1)
                      LIMIT 1);

这是我用来获得两个答案之一的代码。我只是想知道是否有可能以某种方式获得一个以上的答案?

1 个答案:

答案 0 :(得分:1)

首先,您不需要deltakelse子句中的表from。如果没有这个,你可以摆脱distinct

您的问题的答案是将=更改为in

SELECT utover.startnr, utover.navn
FROM utover
WHERE utover.startnr in (SELECT utover
                         FROM deltakelse
                         GROUP BY utover
                         HAVING COUNT(*) = (SELECT COUNT(*)
                                            FROM deltakelse
                                            GROUP BY utover
                                            ORDER BY COUNT(*) DESC
                                            LIMIT 1
                                           )
                        );