我需要帮助一个带有ann ann的查询,我必须为每个健身成员超过5年的用户找到练习“Bench Press”和“Squat”的1 rep max(Max(Carico))。
我试过了:
SELECT U.Nome,U.Cognome, MAX(P1.Carico) AS MaxSquat,MAX(P2.Carico) AS MaxBench
FROM utente AS U, scheda AS S1, programma AS P1,scheda AS S2, programma AS P2
WHERE U.CF IN(SELECT U.CF
FROM Utente U
WHERE Data_Iscrizione < date_sub(curdate(), interval 5 year))
AND U.CF=S1.ID_Utente AND S1.ID_Scheda=P1.ID_Scheda
AND U.CF=S2.ID_Utente AND S2.ID_Scheda=P2.ID_Scheda
AND P1.nRipetizioni=1 AND P1.Esercizio="Bench Press"
AND P2.nRipetizioni=1 AND P2.Esercizio="Squat"
GROUP BY U.Nome,U.Cognome
我将此作为输出:
Nome |Cognome|MaxSquat |MaxBench|
Mario |lalala | 100 | 200 |
Simon |jujuju | 130 | 100 |
但它没有显示具有Null值的元组
我想获得这样的输出:
Nome |Cognome|MaxSquat |MaxBench|
John |blabla | 90 | Null |
Carl |lasdll | Null | 150 |
Mario |lalala | 100 | 200 |
Simon |jujuju | 130 | 100 |
我该怎么做?谢谢
答案 0 :(得分:1)
当您在其中分段中说明条件时,您将只获得这些条件为真的结果。具体来说,你得到了你的结果:P1.Esercizio =&#34; Bench Press&#34;和P2.Esercizio =&#34; Squat&#34; (和其他一些条件)。
你需要的是没有P1的结果或P1的结果.Esercizio =&#34; Bench Press&#34; (以及许多其他条件)
这就是左连接的位置:从指定的第一个表中获取所有行,只从第二个表中获取可以匹配的行。移动 ALL 适用于在join子句的 on 关键字后允许不存在的表的条件
您可以将左连接置于严重状态并编写此
SELECT U.Nome
, U.Cognome
, MAX(P1.Carico) AS MaxSquat
, MAX(P2.Carico) AS MaxBench
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 AND P1.Esercizio="Bench Press"
left join scheda AS S2 on U.CF=S2.ID_Utente
left join programma AS P2 on S2.ID_Scheda=P2.ID_Scheda
AND P2.nRipetizioni=1 AND P2.Esercizio="Squat"
WHERE U.CF IN(SELECT U.CF
FROM Utente U
WHERE Data_Iscrizione < date_sub(curdate(), interval 5 year))
GROUP BY U.Nome, U.Cognome
我没有测试这段代码。如果你无法让它运行,请随时在评论中提及这一点,我会再次接受它。