1

Tengo una tabla playlist, playlist_track, playlist_adversiting. Intento realizar una consulta donde retorne todos los registro de playlist con la cantidad correspondiente a sus otras tablas:

SQL:

SELECT
   playlist.*,
   user_created.user_id as user_created_id,
   user_created.user_name as user_created_name,
   user_updated.user_id as user_updated_id,
   user_updated.user_name  as user_updated_name,
   COUNT( playlist_track.playlist_track_id ) as track_count,
   COUNT( playlist_advertising.playlist_advertising_id ) AS advertising_count

FROM playlist

INNER JOIN user user_created ON user_created.user_id = playlist.playlist_createdBy
INNER JOIN user user_updated ON user_updated.user_id = playlist.playlist_updatedBy

LEFT JOIN playlist_track ON playlist_track.playlist_track_id_playlist = playlist.playlist_id

LEFT JOIN playlist_advertising ON playlist_advertising.playlist_advertising_id_playlist = playlist.playlist_id

GROUP BY
    playlist.playlist_id

1 respuesta 1

2

Lo que tendrias que hacer es un subquery para tener la cantidad.

           (SELECT COUNT(playlist_track.playlist_track_id) 
              FROM playlist_track 
             WHERE playlist_track.playlist_track_id_playlist = playlist.playlist_id) as `# playlist_track`,
           (SELECT COUNT(playlist_advertising.playlist_advertising_id) 
                  FROM playlist_advertising 
             WHERE playlist_advertising.playlist_advertising_id_playlist = playlist.playlist_id) as `# playlist_advertising`
FROM playlist

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.