Mysql
 sql >> база данни >  >> RDS >> Mysql

Основни MySQL заявки

Можете да опитате заявките на адресhttp://sqlfiddle.com/#!9/839d2/1

Заявка 1

Списък на всички планети, които имат капитани. Показва id_planet, planet_name, общия брой капитани на всяка от тези планети, както и броя на битките (ако има такива), които са се провели там.

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

Можете да получите същите резултати, като използвате това:

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

Заявка 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;

Тук не използвах Joins за изчисляване на number_of_soldiers и number_of_battles, както направих при заявка 1.1, защото това би било корелирана подзаявка и като такава не можеше да има достъп до външните заявки (https://dev.mysql.com/doc/refman/5.5/en/from-clause -subqueries.html ).

Грешна заявка:

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

Така че горната заявка е грешна и произвежда грешката:Неизвестна колона „aa.id_planet“ в „клауза where“.

Що се отнася до втората заявка, която поискахте, надявам се някой друг да може да опита.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. mySQL множествена заявка - връща грешка mysql_fetch_array

  2. MySQL:ПЪЛНО ВЪНШНО ПРИСЪЕДИНЕНИЕ - Как да слея една колона?

  3. SQL CREATE TABLE Синтаксис – Изброени от СУБД

  4. WooCommerce:Намиране на продуктите в базата данни

  5. Локална реплика на RDS база данни