0

I have 3 tables:

First one named "deaths", that contain id, name, date, editdate,

Second one named "listFriend", that contain names,

Third one named "listEnemy", that contain names

What i want is to get how many records in "deaths" table is matching names from friend and enemy tables.

What i wrote is 2 query

SELECT count(name) as enemies FROM `deaths` WHERE name IN( SELECT name FROM `listEnemy`)
SELECT count(name) as friends FROM `deaths` WHERE name IN( SELECT name FROM `listFriend`)

Its working how it should, but i would like to have it in one query, to get both values by just one request

1 Answers1

0

You can LEFT JOIN the tables and count how many times the join condition worked

SELECT sum(case when e.name is not null then 1 else 0 end) as enemies,
       sum(case when f.name is not null then 1 else 0 end) as friends
FROM deaths d
LEFT JOIN listEnemy e ON e.name = d.name
LEFT JOIN listFriend f ON f.name = d.name
juergen d
  • 201,996
  • 37
  • 293
  • 362