SQL - script - left join y right join

From Ibbddunq

Left join y right join

Se puede partir de <acto, duracion, artista, cachet> para una función

  select nomActo, duracion, nomArt, cachet
  from formaParte
      natural join artista
  where formaParte.diaFn = 20080615 and formaParte.horaFn = 20
  order by nroOrden;

1. supongamos que la función tiene 5 actos, dos tienen asignado artista y tres no. ¿Qué quiere decir "un formaParte no tiene asignado artista".

2. ¿cuántas filas me va a traer este query? Relacionar con el concepto de "fila correspondiente"

3. Si quisiera que me traiga todos los formaParte de la función, los que no tienen artista asignado también, ¿qué les gustaría que trajera en cachet?

4. Hay una forma de decir exactamente eso en la parte de tabla

  tabla1 left join tabla2

quiere decir "incluime también las filas sin correspondencia de la parte izquierda".

5. Tenemos los los sabores: natural y moñito

  from formaParte
      natural left join artista
  from formaParte
      left join artista on formaParte.nomArt = artista.nomArt

6. Otros ejemplos tranquilos

  • artista, maestro y sus cachets, incluyendo los artistas sin maestro
  • artista y su destreza en cuchillos incluyendo los artistas que no saben cuchillos
  • función, ciudad y provincia incluyendo las funciones sin ciudad asignada.

7. Acá gana sentido asociar los joins, hay que entender bien qué es izquierdo y qué es derecho P.ej. (acá usamos tb join moñito)

  select nomActo, duracion, art.nomArt, art.cachet, maestro.nomArt, maestro.cachet
  from formaParte
      left join
        (artista art join artista maestro on art.nomArt_maestro = maestro.nomArt)
      on formaParte.nomArt = art.nomArt
  where formaParte.diaFn = 20080615 and formaParte.horaFn = 20
  order by nroOrden;

Sobre este ejemplo dos cosas

  • por qué no puedo hacer
  from formaParte
      natural left join
        (artista art join artista maestro on art.nomArt_maestro = maestro.nomArt)

(porque el join de los dos artistas tiene dos nomArt y se confunde, ahí hay que hacer con moñito y entender bien)

  • qué diferencia hay con
  select nomActo, duracion, art.nomArt, art.cachet, maestro.nomArt, maestro.cachet
  from formaParte
      left join
        (artista art left join artista maestro on art.nomArt_maestro = maestro.nomArt)
      on formaParte.nomArt = art.nomArt
  where formaParte.diaFn = 20080615 and formaParte.horaFn = 20
  order by nroOrden;

8. Terminología: left join / right join / inner join (el std) = cross join / outer join Esto sólo por si lo leen x ahi

Personal tools