Alternatex
Alternatex

Reputation: 1552

How do I count the number of instances from 2 columns where a given string appears?

Here's an example table:

games_table
----------------------------------------------------
|   home_team   |   away_team   |   game_outcome   |
----------------------------------------------------
|    Chelsea    |    Arsenal    |     Home wins    |
----------------------------------------------------
|    Everton    |   Liverpool   |     Away wins    |
----------------------------------------------------
|    Arsenal    |    Chelsea    |     Home wins    |
----------------------------------------------------

What I need is the number of times 'Chelsea' appears WHERE game_outcome = 'Home wins' which is 2 in this example.

Normally this would be very easy but I'm stuck because I have to search 2 columns. I really need some insight.

Upvotes: 0

Views: 100

Answers (1)

Andrew
Andrew

Reputation: 14457

SELECT COUNT(*)
FROM   games_table
WHERE  game_outcome = 'Home wins'
AND    (home_team = 'Chelsea' OR away_team = 'Chelsea')

Upvotes: 3

Related Questions