Good evening

I'm running a tipping competition, and am hoping to make the updating slightly less tedious. The code currently reads as follows:


    $query = " SELECT SUM(comptipsterselections.profit) as Profit, comptipsterselections.stable,
       FROM comptipsterselections INNER JOIN comptipsterboard
       ON comptipsterselections.stable=comptipsterboard.stable
       WHERE comptipsterselections.comp = 'aintree 2010'
       GROUP BY comptipsterselections.stable,
       ORDER BY SUM(comptipsterselections.profit) DESC";

    $result = mysql_query($query) or die(mysql_error());
    // Set-up table
       echo "<table class='correctenglish' border='1' cellpadding='4' cellspacing='0' width='75%'>";
       echo "<tr class='toprow'> <th>Stable</th> <th>Daily Profit</th></tr>";

    // Print out result
       while($row = mysql_fetch_array($result)){
       echo "<tr><td>";
       echo "<a href='$link'>";
       echo $row[stable];
       echo "</td><td>";
       echo " &#163;". $row[Profit];
       echo "</td></tr>";
       echo "</table>";

The profit for each player is showing correctly for each player. However, the printed results now show links for each competition the player has taken part in, not just the Aintree 2010 competition: Click Here I thought the WHERE clause would filter out anything that didn't have Aintree 2010 in the comp column, but this isn't the case. Is there any advice for getting this added to the code?

The two tables of data look like this:


Any advice gratefully accepted!

Recommended Answers

I'm guessing that the problem is that you need to join your 2 tables with both stable and comp columns.

Jump to Post

All 4 Replies

I'm guessing that the problem is that you need to join your 2 tables with both stable and comp columns.

Try changing from INNER JOIN to LEFT JOIN as your filter is on the left hand table.

@Hearth: This would still cause a cartesian product. Plus left and right are used when you want to include records that don't have matching keys in the joined table. This is not that case. This is not limiting enough the records joined.

Thank you Adam K - works perfectly now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.