OPTIONS VOTES
option a 3
option b 2
option c 3
我有这个mysql查询,它通过votesCount获取选项和订单,限制1获得最佳答案,但在我的示例中,顶部有两个选项,它们都具有最高值,我想查询到得到这两个选项,不仅是一个,所以我必须摆脱LIMIT 1
Mysql查询是
$query = "SELECT `option` FROM `options` WHERE `pid` = '$pid' AND `votesCount` != '0' ORDER BY `votesCount` DESC LIMIT 1";
任何建议?
答案 0 :(得分:4)
以下是任何SQL方言的标准方法:
select p.*
from poll p
where p.votes = (select max(votes) from poll)
答案 1 :(得分: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;