0

我有这个民意调查的东西,民意调查 A,有 3 个选项让我们说:选项 a 选项 b 选项 c,选项 a 获得 3 票,选项 b 获得 2 票,选项 c 获得 3 票,

    OPTIONS     VOTES 
    option a       3
    option b       2
    option c       3

我有这个 mysql 查询,它通过 votesCount 获取选项和订单,限制 1 以获得最佳答案,但在我的示例中,顶部有两个选项,它们都有最高值,我想要一个查询来获得这两个选项,n 不只是一个,所以我必须摆脱 LIMIT 1

Mysql查询是

$query = "SELECT `option` FROM `options` WHERE `pid` = '$pid' AND `votesCount` != '0' ORDER BY `votesCount` DESC LIMIT 1";

有什么建议吗?

4

2 回答 2

4

这是任何 SQL 方言的标准方式:

select p.*
from poll p
where p.votes = (select max(votes) from poll)
于 2013-05-26T23:09:37.957 回答
0

感谢@Gordon Linoff 的提示,这就是现在的样子

$query = "SELECT `option` FROM `options` WHERE `pid` = '$pid' AND `votesCount` = (SELECT MAX(`votesCount`) FROM `options` WHERE `pid` = '$pid' ORDER BY `votesCount` DESC LIMIT 1)";
    ///Just a DB function, don't mind the 0, i'm using a class
    $res = $db->get_rows($db->select($query),0);
    $merged = array();
    foreach ($res as $r){
        $merged[] = $r->option;
    }
    $merged = implode(',',$merged);
    return $merged;
于 2013-05-26T23:44:18.890 回答