sql >> Database teknologi >  >> RDS >> Mysql

Grundlæggende MySQL-forespørgsler

Du kan prøve forespørgslerne påhttp://sqlfiddle.com/#!9/839d2/1

Forespørgsel 1

Liste over alle planeter, der har kaptajner. Den viser id_planet, planet_name, det samlede antal kaptajner på hver af disse planeter samt antallet af kampe (hvis nogen), der fandt sted der.

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

Du kan få samme resultater ved at bruge denne:

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

Forespørgsel 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;

Her brugte jeg ikke Joins til at beregne antal_af_soldater og antal_kampe, som jeg gjorde ved forespørgsel 1.1, fordi det ville være en korreleret underforespørgsel, og som sådan kunne den ikke have adgang ved de ydre forespørgsler (https://dev.mysql.com/doc/refman/5.5/en/from-clause -subqueries.html ).

Forkert forespørgsel:

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

Så ovenstående forespørgsel er forkert og producerer fejlen:Ukendt kolonne 'aa.id_planet' i 'where clause'.

Med hensyn til den anden forespørgsel, du bad om, håber jeg, at en anden kan give det en chance.




  1. Sådan skriver du joinforespørgsel i Volusion API

  2. Fix Msg 8116 "Argumentdatatypedatoen er ugyldig for argument 1 for substring-funktionen" i SQL Server

  3. Multi-Cloud Full Database Cluster Failover indstillinger for PostgreSQL

  4. Forespørg data fra XML