我現在已經改變了我的答案,並將它寫入新的數據。
首先你必須創建兩個新indexe
ALTER TABLE programma
ADD KEY `idx_nRipetizioni` (`nRipetizioni`,`ID_Scheda`,`Esercizio`);
ALTER TABLE utente
ADD KEY `idx_ata_Iscrizione` (`Data_Iscrizione`);
然後你就可以運行這個。請測試它的結果是一樣的。 如果你想擁有的,而不是0變化,0)到,NULL)在 NULL if語句
,看到了執行時間:-)
SELECT
U.Nome,
U.Cognome,
MAX(IF(P1.Esercizio="Panca Orizzontale",P1.Carico,0)) AS MaxPanca_Orizzontale,
MAX(IF(P1.Esercizio="Squat",P1.Carico,0)) AS MaxSquat,
MAX(IF(P1.Esercizio="Estensioni Bilanciere",P1.Carico,0)) AS MaxEstensioni_Bilanciere,
MAX(IF(P1.Esercizio="Lento Avanti",P1.Carico,0)) AS MaxLento_Avanti
FROM utente AS U
LEFT JOIN scheda AS S1 ON U.CF=S1.ID_Utente
LEFT JOIN programma AS P1 ON S1.ID_Scheda=P1.ID_Scheda
AND P1.nRipetizioni=1
WHERE Data_Iscrizione < date_sub(curdate(), INTERVAL 5 YEAR)
GROUP BY U.Nome,
U.Cognome;
樣品
創建索引
MariaDB [yourschema]> ALTER TABLE programma
-> ADD KEY `idx_nRipetizioni` (`nRipetizioni`,`ID_Scheda`,`Esercizio`);
Query OK, 0 rows affected (0.27 sec)
Records: 0 Duplicates: 0 Warnings: 0
創建索引
MariaDB [yourschema]> ALTER TABLE utente
-> ADD KEY `idx_ata_Iscrizione` (`Data_Iscrizione`);
Query OK, 0 rows affected, 1 warning (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 1
運行查詢
MariaDB [yourschema]> SELECT
-> U.Nome,
-> U.Cognome,
-> MAX(IF(P1.Esercizio="Panca Orizzontale",P1.Carico,0)) AS MaxPanca_Orizzontale,
-> MAX(IF(P1.Esercizio="Squat",P1.Carico,0)) AS MaxSquat,
-> MAX(IF(P1.Esercizio="Estensioni Bilanciere",P1.Carico,0)) AS MaxEstensioni_Bilanciere,
-> MAX(IF(P1.Esercizio="Lento Avanti",P1.Carico,0)) AS MaxLento_Avanti
-> FROM utente AS U
-> LEFT JOIN scheda AS S1 ON U.CF=S1.ID_Utente
-> LEFT JOIN programma AS P1 ON S1.ID_Scheda=P1.ID_Scheda
-> AND P1.nRipetizioni=1
-> WHERE Data_Iscrizione < date_sub(curdate(), INTERVAL 5 YEAR)
-> GROUP BY U.Nome,
-> U.Cognome;
+------------+--------------+----------------------+----------+--------------------------+-----------------+
| Nome | Cognome | MaxPanca_Orizzontale | MaxSquat | MaxEstensioni_Bilanciere | MaxLento_Avanti |
+------------+--------------+----------------------+----------+--------------------------+-----------------+
| Ajeje | Brazov | 0.0 | 100.0 | 0.0 | 35.0 |
| Aldo | Baglio | 80.0 | 120.0 | 32.5 | 50.0 |
| Fernando | Torres | 0.0 | 150.0 | 0.0 | 35.0 |
| Francesco | Toldo | 90.0 | 0.0 | 40.0 | 0.0 |
| Giovanni | Storti | 65.0 | 0.0 | 0.0 | 30.0 |
| Guendalina | Porte | 0.0 | 50.0 | 20.0 | 25.0 |
| Harry | Potter | 150.0 | 180.0 | 80.0 | 122.5 |
| John | Cena | 135.0 | 240.0 | 60.0 | 75.5 |
| Kevin | Velociraptor | 0.0 | 0.0 | 20.0 | 95.0 |
| Luciano | Spalletti | 60.0 | 280.0 | 95.0 | 100.0 |
| Marcella | Mandria | 0.0 | 50.0 | 0.0 | 27.5 |
| Marcelo | Zalayeta | 140.0 | 200.0 | 55.0 | 60.0 |
| Radja | Nainggolan | 90.0 | 120.0 | 0.0 | 40.0 |
| Romina | Power | 0.0 | 0.0 | 140.0 | 20.0 |
+------------+--------------+----------------------+----------+--------------------------+-----------------+
14 rows in set (0.00 sec)
MariaDB [yourschema]>
可以提供tablestructures和一些示例數據? – Philipp
...和期望的結果。 – Strawberry
添加了所需的結果,現在我嘗試添加創建表 – LucaPearl