sql >> Database >  >> RDS >> Mysql

Basis MySQL-query's

U kunt de query's proberen ophttp://sqlfiddle.com/#!9/839d2/1

Vraag 1

Lijst van alle planeten die kapiteins hebben. Het toont id_planet, planet_name, het totale aantal kapiteins op elk van die planeten en het aantal veldslagen (indien aanwezig) dat daar plaatsvond.

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';

U kunt dezelfde resultaten krijgen door dit te gebruiken:

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';

Vraag 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;

Hier heb ik Joins niet gebruikt om number_of_soldiers en number_of_battles te berekenen zoals ik deed bij Query 1.1 omdat dat een gecorreleerde subquery zou zijn en als zodanig geen toegang zou hebben tot de buitenste queries (https://dev.mysql.com/doc/refman/5.5/en/from-clause -subqueries.html ).

Verkeerde zoekopdracht:

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';

De bovenstaande query is dus fout en produceert de fout:Onbekende kolom 'aa.id_planet' in 'where-clausule'.

Wat betreft de tweede vraag waar je om vroeg, hoop ik dat iemand anders het kan proberen.




  1. Index gebruiken, tijdelijk gebruiken, filesort gebruiken - hoe dit op te lossen?

  2. MySQL-rangschikking in het geval van banden

  3. De PIVOT-functie in T-SQL begrijpen

  4. Foutcode:1062. Dubbele invoer '1' voor sleutel 'PRIMARY'