I have a table which outputs all the sql results from Table 1. Its primary column (stationName) has all unique names. I have added columns to this new table to also include results (scores) from Table 2. The below query yields results which are spread across new lines, creating duplicates of the same name.
$query = "SELECT * FROM table1
LEFT JOIN table2 ON table1.stationName = table2.stationName";
$stationsList = mysqli_query($link, $query);
<?php while($row = mysqli_fetch_array($stationsList)):;?>
<tr>
<td><?php echo $row[table1ItemA];?></td>
<td><?php echo $row[table1ItemB];?></td>
<td><?php echo $row[table1ItemC];?></td>
<td><?php echo $row[table1ItemD];?></td>
<td><?php echo $row[table2score1];?></td>
<td><?php echo $row[table2score2];?></td>
<td><?php echo $row[table2score3];?></td>
</tr>
<?php endwhile;?>
Outputs the score columns as:
Name A Score 1
Name A .............Score 2
Name A ............................Score 3
What I would like to achieve is:
Name A Score 1, Score 2, Score 3.
GROUP BY does not work, as it returns only one score for the three columns.
Doing some research, I believe I need to use DISTINCT.
However, I still need to be able to SELECT * from table1, or at least be able to include all of it in the combined table.
Can anyone advise on the best way to achieve this please?
Edit:
To better show the two tables and what I would like to achieve combined.
Table 1 (Fixed Info)
+-------------+------------+---------+
| StationName | Address | Manager |
+-------------+------------+---------+
| Station1 | London | John |
| Station2 | Liverpool | Phil |
| Station3 | Manchester | Mike |
+-------------+------------+---------+
Table 2 (Varies Monthly)
+-------------+--------+--------+--------+
| StationName | Score1 | Score2 | Score3 |
+-------------+--------+--------+--------+
| Station1 | Pass | Fail | Pass |
| Station2 | Fail | Pass | Pass |
| Station3 | Pass | Pass | Pass |
+-------------+--------+--------+--------+
Combined Table as I would like it to appear:
+-------------+------------+---------+--------+--------+--------+
| StationName | Address | Manager | Score1 | Score2 | Score3 |
+-------------+------------+---------+--------+--------+--------+
| Station1 | London | John | Pass | Fail | Pass |
| Station2 | Liverpool | Phil | Fail | Pass | Pass |
| Station3 | Manchester | Mike | Pass | Pass | Pass |
+-------------+------------+---------+--------+--------+--------+
How it appears:
+-------------+------------+---------+--------+--------+--------+
| StationName | Address | Manager | Score1 | Score2 | Score3 |
+-------------+------------+---------+--------+--------+--------+
| Station1 | London | John | Pass | | |
| Station1 | London | John | | Fail | |
| Station1 | London | John | | | Pass |
| Station2 | Liverpool | Phil | Fail | | |
| Station2 | Liverpool | Phil | | Pass | |
| Station2 | Liverpool | Phil | | | Pass |
| Station3 | Manchester | Mike | Pass | | |
| Station3 | Manchester | Mike | | Pass | |
| Station3 | Manchester | Mike | | | Pass |
+-------------+------------+---------+--------+--------+--------+