Skip to main content
edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Combining two Counting penalties for each player by joining tables, where some of the data is null

Source Link

Combining two tables where some of the data is null

I have a table full of player names:

enter image description here

I also have a table full of player penalties:

enter image description here

I have to list all of the players names, player numbers, and the number of penalties that they've incurred.

To do that, I'm using this code:

select players.playerno, name, ifnull(`Penalty count`, 0) as `Penalty count` from players
left join
(select players.playerno, count(players.playerno) as `Penalty count` from players, penalties
where players.playerno = penalties.playerno
group by players.playerno) as `query1`
on players.playerno = query1.playerno
order by playerno

The code works. Here's the output table:

enter image description here

It seems like a lot of code just to merge two tables together. Is there a better way to do it?

I've also read online that keeping the NULL is generally a better practice than changing it to zero, but would that apply in this case, since it's supposed to be the total number of penalties? It seems like NULL would give the impression of "no data" or "unknown data", but in this case the number is known to be zero.