skip to Main Content

I essentially need to pull out a leadership board if you like of memberID, their first names, last names and their result and order them into highest first.

So far I have the first names and surnames appearing and then results however my query seems to be incorrect as it’s showing all possible names, with all possible results as seen below (I know I need a join, not sure how though and which one):

Incorrect Results

Below I have actually posted what the results look like within the table:

What phpMyAdmin shows for results

The info and tables needed below are:

  • tblMember holds the fldMemberID, fldFName, fldSName
  • tblMembEntComp holds the fldResult.

So far I have this, but it’s not quite right as you can see on my first screenshot.

<div class="grid-2"> 
    <p><b>LEADERSHIP BOARD</b></p>
    <?php
        $query = "SELECT `tblMember`.`fldFName`, `tblMember`.`fldSName`, `tblMembEntComp`.`fldResult` FROM `tblMember`, `tblMembEntComp`";

        $result = $conn -> query($query);

        while($row = $result -> fetch_assoc()) 
            {
                echo $row['fldFName']." ".$row['fldSName']." ".$row['fldResult']."<br>";
            } 
    ?>
</div>

2

Answers


  1. You need to specify the columns that relate the two tables:

    SELECT m.fldFName, m.fldSName, e.fldResult
    FROM tblMember AS m
    JOIN tblMembEntComp as e ON m.fldMemberID = e.fldMemberID
    

    Otherwise you get a full cross product between the two tables.

    Login or Signup to reply.
  2. Use for example LEFT JOIN

    SELECT T1.*, T2.*
    FROM tblMember T1
    LEFT JOIN tblMembEntComp T2
    ON T1.UniqueCommonField = T2.UniqueCommonField;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search