简体   繁体   中英

MySQL/PHP Grouping results based on date range and user id

Hello so here is my dilema. I have 2 tables, one called users and user_betting. Users i was using to innerjoin their name to the results in the link below. Here is the table structure of 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

If you browse this page. http://wewatchcappers.com/rankings_full.php?sport=MLB you can see currently I can pull the data for 30 / 60 / 90 days. However my method of doing so doesn't allow me to sort highest points. Basically what needs to happen is I need to count the number of times they have the word win / tie / loss in any of the *_decision. The points will need to be the sum of the *_awarded. These could be negative and positive numbers.

I believe I will need to input this into an array and then sort it but I do not know the best way to go about this. Can anyone give me help here?

Here is the current way I have it done.

<?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 } ?>

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

You want to substract the length of all the texts without the word "win" to the length of all the texts with the word "win".

sum of all text - sum of text without "win"

That will give you the number of "win" characters you have, which you should divide by 3 to know how many time the word is said.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM