你好,这是我的困境。我有2个表,一个叫user和user_betting。我正在使用的用户将他们的名字加入到以下链接中的结果中。这是user_betting的表结构。
id
handicapper_id
game_id
sport
date_placed
ML_team
ML_decision
ML_points
ML_awarded
OU_team
OU_decision
OU_points
OU_awarded
SPREAD_team
SPREAD_decision
SPREAD_points
SPREAD_awarded
如果您浏览此页面。 http://wewatchcappers.com/rankings_full.php?sport=MLB您目前可以看到我可以将数据提取30/60/90天。但是我这样做的方法并不能让我排序最高分。基本上需要发生的是我需要计算他们在任何* _decision中获得单词win / tie / loss的次数。这些点必须是* _awarded的总和。这些可能是负数和正数。
我相信我需要将它输入一个数组然后对其进行排序,但我不知道最好的方法。谁能帮到我这里?
这是我目前完成的方式。
<?php foreach($conn->query("SELECT * FROM users INNER JOIN user_betting ON users.id=user_betting.handicapper_id WHERE sport = '$sport' AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() GROUP BY user_betting.handicapper_id") as $rankings) { ?>
<tr>
<td><a href="<?php echo $rankings['username'];?>"><?php echo $rankings['username']; ?></a></td>
<td align="center">
<?php
$ml30wins = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND ML_decision = 'win' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() ")->fetchColumn();
$ou30wins = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND OU_decision = 'win' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() ")->fetchColumn();
$spread30wins = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND SPREAD_decision = 'win' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() ")->fetchColumn();
echo $ml30wins+$ou30wins+$spread30wins;
?>
</td>
<td align="center">
<?php
$ml30loss = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND ML_decision = 'loss' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()")->fetchColumn();
$ou30loss = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND OU_decision = 'loss' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()")->fetchColumn();
$spread30loss = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND SPREAD_decision = 'loss' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()")->fetchColumn();
echo $ml30loss+$ou30loss+$spread30loss;
?></td>
<td align="center">
<?php
$ml30ties = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND ML_decision = 'tie' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW() ")->fetchColumn();
$ou30ties = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND OU_decision = 'tie' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()")->fetchColumn();
$spread30ties = $conn->query("SELECT count(*) FROM user_betting WHERE sport = '$sport' AND SPREAD_decision = 'tie' AND handicapper_id = ".$rankings['handicapper_id']." AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()")->fetchColumn();
echo $ml30ties+$ou30ties+$spread30ties;
?>
</td>
<td align="center">
<?php
$ml30query = mysql_query('SELECT SUM(ML_awarded) AS ml30_sum FROM user_betting WHERE handicapper_id = '.$rankings['handicapper_id'].' AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()');
$ml30row = mysql_fetch_assoc($ml30query);
$ml30sum = $ml30row['ml30_sum'];
$ou30query = mysql_query('SELECT SUM(OU_awarded) AS ou30_sum FROM user_betting WHERE handicapper_id = '.$rankings['handicapper_id'].' AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()');
$ou30row = mysql_fetch_assoc($ou30query);
$ou30sum = $ou30row['ou30_sum'];
$spread30query = mysql_query('SELECT SUM(SPREAD_awarded) AS spread30_sum FROM user_betting WHERE handicapper_id = '.$rankings['handicapper_id'].' AND date_placed BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW()');
$spread30row = mysql_fetch_assoc($spread30query);
$spread30sum = $spread30row['spread30_sum'];
echo round($ml30sum+$ou30sum+$spread30sum,2);
?>
</td>
</tr>
<?php } ?>
答案 0 :(得分:0)
SELECT (SUM(LEN(OU_DECISION),LEN(ML_DECISION),LEN(SPREAD_DECISION)) - SUM(LEN(REPLACE(OU_DECISION, 'win', '')),LEN(REPLACE(SPREAD_DECISION, 'win', '')),LEN(REPLACE(ML_DECISION, 'win', ''))))/3
你想要减去所有文本的长度而没有&#34; win&#34;使用&#34; win&#34;。
这个词的所有文本的长度sum of all text - sum of text without "win"
这将为您提供&#34; win&#34;你拥有的角色,你应该除以3来知道这个词被说了多少时间。