lunes, 7 de octubre de 2024

inner and left join 3 tables

 $query_ivr = "

    SELECT

        c.cliente_id,

        c.cliente_nombre,

        c.cliente_telefono,

        c.cliente_blacklist_status,

        SUM(co.compra_precio) AS total_compra,

        ivr.ivr_num_src,

        ivr.ivr_num_dst,

        ivr.ivr_caller_channel,

        ivr.ivr_call_status,

        ivr.ivr_calldate,

        b.balance_creditos

    FROM

        clientes AS c

    INNER JOIN

        compras AS co ON c.cliente_id = co.compra_cliente_id

    INNER JOIN

        ivr_calls AS ivr ON ivr.ivr_num_src = c.cliente_telefono AND ivr.ivr_call_status = 1

    LEFT JOIN

        balance AS b ON b.balance_cliente = c.cliente_id

    GROUP BY

        c.cliente_id,

        c.cliente_nombre,

        c.cliente_telefono,

        c.cliente_blacklist_status,

        ivr.ivr_num_src,

        ivr.ivr_num_dst,

        ivr.ivr_caller_channel,

        ivr.ivr_call_status,

        ivr.ivr_calldate,

        b.balance_creditos;";


  1. Added a LEFT JOIN:

    • The balance table is joined with LEFT JOIN to ensure that even if there is no corresponding entry in the balance table, you still get results from the other tables.
  2. Selected b.balance_creditos:

    • This column is included in the SELECT statement to retrieve the credit balance for each client.

Explanation:

  • Using a LEFT JOIN allows you to fetch client information even if they do not have any associated balance records.

No hay comentarios:

Publicar un comentario