我有一个如下的MySQL查询;我想在具有180万条记录的表中为600条记录的每个范围选择最高记录。到目前为止,我必须循环3,000次才能完成此操作,但这不是有效的解决方案。
数据库架构;
Table: bet_perm_13predict
id bet_id perm_id avg_odd avg_odd2 avg_odd3
1 23 1 43.29 28.82 28.82
2 23 2 42.86 28.59 28.59
3 23 3 43.13 28.73 28.73
Table: bet_permute_13games
perm_id perm_code
1 0000000000000
2 0000000000001
3 0000000000002
4 0000000000010
用PHP示例MySQL查询
$totRange = 0; //Used as starting point in rang
$range = 600; //Used as range
$stop = 0;//Used as endPoint of range
while($totRange < 1800000){
$stop = $totRange+$range;
$sql = "SELECT (tb1.avg_odd2 + tb1.avg_odd3) AS totAvg_odd ,
tb1.perm_id , tb1.avg_odd, tb1.avg_odd2, tb1.avg_odd3, tb2.perm_code
FROM bet_perm_13predict tb1
INNER JOIN bet_permute_13games tb2 ON tb2.perm_id = tb1.perm_id
WHERE tb1.bet_id = '$bet_id' && tb1.perm_id
BETWEEN $startRange AND $stop ORDER BY totAvg_odd ASC LIMIT 1"
$q1 = $this->db->query($sql);
$totRange = $stop;
}
换句话说,我想选择一个表示整个表的数据样本,该样本不是随机的,而是使用600范围内的最高记录预定义的。到目前为止,我不知道如何进行。没有关于此主题的清晰在线材料。
答案 0 :(得分:1)
您可以使用整数除法创建组。
<div class="collection__list j-scrollElement" data-type="Other" data-showTimestamp="True" data-showImage="False" data-showProvider="True" data-showByline="False" >
<div data-docid="1341791639" class="element element--article no-image j-scrollByElement">
<div class="article__content">
<h3 class="article__headline"><a class="link" rel="nofollow" target="_blank" data-source="seekingalpha.com" href="https://seekingalpha.com/news/3392514-new-age-beverages-enters-market-offering-agreement?source=marketwatch">New Age Beverages enters into At the Market Offering Agreement</a></h3>
<p class="article__summary">New Age Beverages enters into At the Market Offering Agreement</p>
<ul class="article__details">
<li data-est="2018-09-24T13:51:00" class="article__timestamp">Sep. 24, 2018 at 1:51 p.m. ET</li>
<li class="article__provider">on Seeking Alpha</li>
</ul>
</div>
</div>
<div data-docid="1341775218" class="element element--article no-image j-scrollByElement">
<div class="article__content">
<h3 class="article__headline"><a class="link" rel="nofollow" target="_blank" data-source="fool.com" href="https://www.fool.com/investing/2018/09/24/big-highs-wont-last-for-new-age-beverage-stock.aspx?source=djc&utm_campaign=article&utm_medium=feed&utm_source=djc">Big Highs Won't Last for New Age Beverage Stock</a></h3>
<p class="article__summary"></p>
<ul class="article__details">
<li data-est="2018-09-24T09:00:00" class="article__timestamp">Sep. 24, 2018 at 9:00 a.m. ET</li>
<li class="article__provider">on Motley Fool</li>
</ul>
</div>
然后找到每个组的最大值。一些选项
Get records with max value for each group of grouped SQL results
答案 1 :(得分:0)
对于那些可能遇到相同问题的人,这就是我解决的方法。我使用@Juan Carlos建议,并添加了一种使用Subquery来选择组的最高记录的方法。
SELECT * FROM
(SELECT * , perm_id DIV $limit as grp , (avg_odd2 + avg_odd3) AS totAvg_odd
FROM bet_perm_13predict WHERE bet_id = '$bet_id' ORDER BY grp ASC ) tb1
INNER JOIN bet_permute_13games tb2 ON tb2.perm_id = tb1.perm_id
INNER JOIN bet_entry tb3 ON tb3.bet_id = tb1.bet_id
WHERE tb1.avg_odd2 < (SELECT AVG(avg_odd2) FROM bet_perm_13predict WHERE bet_id = '$bet_id' )
&& tb1.avg_odd3 < (SELECT AVG(avg_odd3) FROM bet_perm_13predict WHERE bet_id = '$bet_id' )
GROUP BY grp ORDER BY totAvg_odd ASC
LIMIT 100