-1

With the next database: G

I need to select all soldiers who have been ONLY in battles in their own planet and I don't have any idea of how can I make it.

This is what I've tried.

SELECT id_soldier, name 
FROM soldier, battle_log, battle 
WHERE IN(...soldier.planet_id = battle.id_planet_battle...) 
hlh3406
  • 1,382
  • 5
  • 29
  • 46
therealbigpepe
  • 1,489
  • 2
  • 16
  • 23

3 Answers3

2

Since I don't have access to the db I can't test it but can you give this a try?

SELECT id_soldier, name 
FROM soldier s 
LEFT JOIN battle_log l ON s.id_soldier = l.soldier_id 
LEFT JOIN battle b ON l.battle_id = b.id_battle 
WHERE s.planet_id = b.id_planet_battle

If it returns an error just let me know and we'll try to fix it.

nowhere
  • 1,558
  • 1
  • 12
  • 31
2

You can use the not exists operator to exclude soldiers who participated in battles on other planets:

SELECT *
FROM   soldier s
WHERE  NOT EXISTS (SELECT *
                   FROM   battle b
                   JOIN   battle_log bl ON b.id_battle = bl.battle_id
                   WHERE  bl.soldier_id = s.id_soldier AND
                          b.id_planet_battle != s.planet_id)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

enter image description here

I think you can use simple query and join this step.

If I understand correctly soldier join planet (soldier have own planet) Then join Battle by planet (because planet that soldier have it's have battle)

Chanom First
  • 1,136
  • 1
  • 11
  • 25