I need to make several queries, I'm having troubles when I try to:
List of all planets showing the soldiers with a captain rank and the number of the battles in each planet.
ID_PLANET | PLANET_NAME | CAPTAINS COUNT | BATTLES COUNT
SELECT id_planet, planet_name , count(rank) FROM planet INNER JOIN soldier ON planet_id = id_planet WHERE rank = 'Captain';
List of all soldiers who have been ONLY in a war of their own planet.
ID_SOLDIER | NAME_SOLDIER
SELECT id_soldier, name FROM soldier INNER JOIN planet ON planet_id = id_planet INNER JOIN battle ON id_planet = id_planet_battle WHERE planet_id = id_planet_battle;
List of soldiers including the next:
*NAME | RANK | PLANET_FROM | NUMBER OF SOLDIERS FROM HOME PLANET | BATTLES *
Working on it.
My attemps are a disaster, so after two days trying, here I am, asking for help.
You can try the queries at http://sqlfiddle.com/#!9/839d2/1
Query 1
List of all planets that have captains. It shows id_planet, planet_name, total captains on each of those planets as well as the number of battles (if any) that took place there.
SELECT DISTINCT aa.id_planet, aa.planet_name, _aa.captains_count, _bb.battles_count
FROM planet AS aa
INNER JOIN soldier AS bb
ON aa.id_planet = bb.planet_id
INNER JOIN (
SELECT planet_id, COUNT(*) AS captains_count
FROM soldier
WHERE rank = 'captain'
GROUP BY planet_id
) AS _aa
ON aa.id_planet = _aa.planet_id
LEFT JOIN (
SELECT id_planet_battle, COUNT(*) AS battles_count
FROM battle
GROUP BY id_planet_battle
) AS _bb
ON aa.id_planet = _bb.id_planet_battle
WHERE bb.rank = 'captain';
You can have same results using this:
SELECT DISTINCT aa.id_planet, aa.planet_name,
(
SELECT COUNT(*)
FROM soldier AS _aa
WHERE _aa.rank = 'captain' AND aa.id_planet = _aa.planet_id
GROUP BY _aa.planet_id
) AS captains_count,
(
SELECT COUNT(*)
FROM battle AS _bb
WHERE aa.id_planet = _bb.id_planet_battle
GROUP BY _bb.id_planet_battle
) AS battles_count
FROM planet AS aa
INNER JOIN soldier AS bb
ON aa.id_planet = bb.planet_id
WHERE bb.rank = 'captain';
Query 3
SELECT aa.name, aa.rank, bb.planet_name AS planet_from, (
SELECT COUNT(*)
FROM soldier AS _aa
WHERE _aa.planet_id = aa.planet_id
) AS number_of_soldiers,
(
SELECT COUNT(*)
FROM battle AS _bb
WHERE _bb.id_planet_battle = aa.planet_id
) AS number_of_battles
FROM soldier AS aa
INNER JOIN planet AS bb
ON aa.planet_id = bb.id_planet;
Here, i didn't use Joins to calculate number_of_soldiers and number_of_battles as i done at Query 1.1 because that would be a correlated subquery and as such it couldn't have access at the outer queries (https://dev.mysql.com/doc/refman/5.5/en/from-clause-subqueries.html).
Wrong query:
SELECT DISTINCT aa.id_planet, aa.planet_name, _aa.captains_count, _bb.battles_count
FROM planet AS aa
INNER JOIN soldier AS bb
ON aa.id_planet = bb.planet_id
INNER JOIN (
SELECT COUNT(*) AS captains_count
FROM soldier AS _aa
WHERE _aa.rank = 'captain' AND aa.id_planet = _aa.planet_id
GROUP BY _aa.planet_id
) AS _aa
ON aa.id_planet = _aa.planet_id
LEFT JOIN (
SELECT COUNT(*) AS battles_count
FROM battle AS _bb
WHERE aa.id_planet = _bb.id_planet_battle
GROUP BY _bb.id_planet_battle
) AS _bb
ON aa.id_planet = _bb.id_planet_battle
WHERE bb.rank = 'captain';
So the above query is wrong and produces the error: Unknown column 'aa.id_planet' in 'where clause'.
As for the second query you asked for i hope someone else could give it a try.
来源:https://stackoverflow.com/questions/32877745/basic-mysql-queries