Foros del Web » Programación para mayores de 30 ;) » Bases de Datos General » Mysql »

[SOLUCIONADO] Consulta MySQL Habitaciones Hotel

Estas en el tema de Consulta MySQL Habitaciones Hotel en el foro de Mysql en Foros del Web. Ante todo un cordial saludo a todos. Tengo la siguiente consulta de mysql: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código SQL: Ver original SELECT tbl_fechas_habitaciones . idFecha_Habitacion , tbl_fechas_habitaciones ...
  #1 (permalink)  
Antiguo 18/12/2015, 08:50
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Consulta MySQL Habitaciones Hotel

Ante todo un cordial saludo a todos.
Tengo la siguiente consulta de mysql:
Código SQL:
Ver original
  1. SELECT
  2. tbl_fechas_habitaciones.idFecha_Habitacion,
  3. tbl_fechas_habitaciones.refHabitacion,
  4. tbl_fechas_habitaciones.fechas,
  5. tbl_hotel.strNombre,
  6. tbl_hotel.intCategoria,
  7. tbl_habitaciones_hotel.refTipo_Habitacion,
  8. tbl_habitaciones_hotel.refImagenes,
  9. tbl_habitaciones_hotel.intEstado,
  10. tbl_habitaciones_hotel.intNumero_Habitacion,
  11. tbl_habitaciones_hotel.refHotel,
  12. tbl_habitaciones_hotel.id_Habitacion,
  13. tbl_hotel.idHotel,
  14. tbl_habitaciones_hotel.refUsuario,
  15. tbl_habitaciones_hotel.intPrecio_Especial,
  16. tbl_habitaciones_hotel.intPrecio_Alta,
  17. tbl_habitaciones_hotel.intPrecio_Media,
  18. tbl_habitaciones_hotel.intPrecio_Baja,
  19. tbl_habitaciones_hotel.strDescuento,
  20. tbl_habitaciones_hotel.intDescuento_Estado,
  21. tbl_habitaciones_hotel.intPromocion,
  22. tbl_habitaciones_hotel.strPromocion_Contenido,
  23. tbl_habitaciones_hotel.strDescripcion,
  24. tbl_habitaciones_hotel.intAdultos,
  25. tbl_habitaciones_hotel.intNinos,
  26. tbl_habitaciones_hotel.intBebes,
  27. tbl_hotel.intEstado,
  28. tbl_hotel.dblLatitud,
  29. tbl_hotel.dblLongitud,
  30. tbl_hotel.idProvincia,
  31. tbl_hotel.strPoblacion,
  32. tbl_hotel.strResumen,
  33. tbl_hotel.intNumero_Habitaciones,
  34. tbl_hotel.strEmail,
  35. tbl_hotel.strTelefono
  36. FROM tbl_fechas_habitaciones
  37. INNER JOIN tbl_habitaciones_hotel ON tbl_fechas_habitaciones.refHabitacion = tbl_habitaciones_hotel.id_Habitacion
  38. INNER JOIN tbl_hotel ON tbl_habitaciones_hotel.refHotel = tbl_hotel.idHotel
  39. WHERE tbl_fechas_habitaciones.fechas BETWEEN '2015-12-18' AND '2015-12-28'
  40. AND tbl_hotel.intEstado = 1 AND tbl_habitaciones_hotel.intEstado = 1
  41. AND tbl_hotel.idProvincia = 43
  42. GROUP BY tbl_fechas_habitaciones.refHabitacion
[/CODE]
Que me arroja este resultado:
refHabitacion Fecha
2 2015-12-18

En la tabla correspondiente hay 3 Habitaciones la habitación id 2 tiene reservados desde 2015-12-18 hasta 2015-12-31
Las habitaciones id 1 y id 3 no tienen reservados esos días, los tiene libres.

Como podría hacer para que en la consulta anterior me saliera este resultado:
refHabitacion Fecha
1 2015-12-18
3 2015-12-18

Muchas gracias de antemano, un saludo: Manyblue

Última edición por gnzsoloyo; 18/12/2015 a las 09:49
  #2 (permalink)  
Antiguo 18/12/2015, 09:13
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 7 meses
Puntos: 774
Respuesta: Consulta MySQL Habitaciones Hotel

usa left join en lugar de inner join :)
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #3 (permalink)  
Antiguo 18/12/2015, 11:01
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Respuesta: Consulta MySQL Habitaciones Hotel

LEFT JOIN tbl_habitaciones_hotel ON tbl_fechas_habitaciones.refHabitacion = tbl_habitaciones_hotel.id_Habitacion
LEFT JOIN tbl_hotel ON tbl_habitaciones_hotel.refHotel = tbl_hotel.idHotel

Nada, me sigue saliendo:
refHabitacion Fecha
2 2015-12-18

Y me deberia salir:
refHabitacion Fecha
1 2015-12-18
3 2015-12-18

No se muy bien pero creo que la cosa va por NOT IN, no lo se bien por esto os lo pregunto. Muchas gracias por tus molestias.
Un saludo: Manyblue
  #4 (permalink)  
Antiguo 18/12/2015, 11:44
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Respuesta: Consulta MySQL Habitaciones Hotel

He intentado esto pero no sale:

INNER JOIN tbl_habitaciones_hotel ON tbl_fechas_habitaciones.refHabitacion = tbl_habitaciones_hotel.id_Habitacion
INNER JOIN tbl_hotel ON tbl_habitaciones_hotel.refHotel = tbl_hotel.idHotel
WHERE tbl_fechas_habitaciones.fechas BETWEEN '2015-12-18' AND '2015-12-28'
AND tbl_fechas_habitaciones.fechas = ''
AND tbl_hotel.intEstado = 1 AND tbl_habitaciones_hotel.intEstado = 1
AND tbl_hotel.idProvincia = 43
GROUP BY tbl_fechas_habitaciones.refHabitacion

Un saludo: Manyblue
  #5 (permalink)  
Antiguo 18/12/2015, 12:04
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Respuesta: Consulta MySQL Habitaciones Hotel

Me han dicho por ahí que intente esto pero no se como:

AND tbl_fechas_habitaciones.fechas NOT IN(tbl_fechas_habitaciones.fechas)

en un ID de las habitaciones, de forma que NOT IN (SELECT habitaciones que no me interesan)

Un saludo: Manyblue
  #6 (permalink)  
Antiguo 18/12/2015, 12:27
Avatar de Libras
Colaborador
 
Fecha de Ingreso: agosto-2006
Ubicación: En la hermosa perla de occidente
Mensajes: 7.412
Antigüedad: 17 años, 7 meses
Puntos: 774
Respuesta: Consulta MySQL Habitaciones Hotel

el not in no te funciona, puedes poner un ejemplo de tus datos??? no la estructura de tus tablas sino un ejemplo de los datos....
__________________
What does an execution plan say to t-sql query? Go f**k yourself, if you are not happy with me
  #7 (permalink)  
Antiguo 18/12/2015, 12:41
Avatar de gnzsoloyo
Moderador criollo
 
Fecha de Ingreso: noviembre-2007
Ubicación: Actualmente en Buenos Aires (el enemigo ancestral)
Mensajes: 23.324
Antigüedad: 16 años, 4 meses
Puntos: 2658
Respuesta: Consulta MySQL Habitaciones Hotel

Tengamos claro que además del JOIN, tienes tres condiciones obligatorias en tu consutla:
Código MySQL:
Ver original
  1. WHERE tbl_fechas_habitaciones.fechas BETWEEN '2015-12-18' AND '2015-12-28'
  2. AND tbl_hotel.intEstado = 1
  3. AND tbl_habitaciones_hotel.intEstado = 1
  4. AND tbl_hotel.idProvincia = 43
Esto es importante porque sin ver los datos completos de los dos casos que mencionas es ,imposible saber si esos casos cumplen estas condiciones... SI no las cumplen, simplemente no los devolverá, y eso no sería un error.

Por eso necesitamos los datos reales.

Por otro lado, dos consejos iniciales: 1) Aprende a usar alias para las tablas. Si usas los nombres completos de las mismas en el SELECT se vuelve ilegible. 2) Pon las tablas en el orden jerarquico correcto. De las mas importante a la tabla dependiente final. NO se ponen en cualquier orden.

Ejemplo:

Código MySQL:
Ver original
  1.     FH.idFecha_Habitacion,
  2.     FH.refHabitacion,
  3.     FH.fechas,
  4.     HO.strNombre,
  5.     HO.intCategoria,
  6.     HH.refTipo_Habitacion,
  7.     HH.refImagenes,
  8.     HH.intEstado,
  9.     HH.intNumero_Habitacion,
  10.     HH.refHotel,
  11.     HH.id_Habitacion,
  12.     HO.idHotel,
  13.     HH.refUsuario,
  14.     HH.intPrecio_Especial,
  15.     HH.intPrecio_Alta,
  16.     HH.intPrecio_Media,
  17.     HH.intPrecio_Baja,
  18.     HH.strDescuento,
  19.     HH.intDescuento_Estado,
  20.     HH.intPromocion,
  21.     HH.strPromocion_Contenido,
  22.     HH.strDescripcion,
  23.     HH.intAdultos,
  24.     HH.intNinos,
  25.     HH.intBebes,
  26.     HO.intEstado,
  27.     HO.dblLatitud,
  28.     HO.dblLongitud,
  29.     HO.idProvincia,
  30.     HO.strPoblacion,
  31.     HO.strResumen,
  32.     HO.intNumero_Habitaciones,
  33.     HO.strEmail,
  34.     HO.strTelefono
  35.     tbl_hotel HO
  36.         INNER JOIN
  37.     tbl_habitaciones_hotel HH ON HH.refHotel = HO.idHotel
  38.         INNER JOIN
  39.     tbl_fechas_habitaciones FH ON FH.refHabitacion = HH.id_Habitacion
  40.     FH.fechas BETWEEN '2015-12-18' AND '2015-12-28'
  41.         AND HO.intEstado =  HH.intEstado
  42.         AND HO.intEstado = 1
  43.         AND HO.idProvincia = 43
  44. GROUP BY FH.refHabitacion

Solo me asalta una duda... ¿Realmente creas un registro en la tabla "tbl_fechas_habitaciones" por cada día reservado? ¿Es correcto? ¿No es un registro con periodo desde/hasta en la reserva?
__________________
¿A quién le enseñan sus aciertos?, si yo aprendo de mis errores constantemente...
"El problema es la interfase silla-teclado." (Gillermo Luque)
  #8 (permalink)  
Antiguo 18/12/2015, 13:28
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Respuesta: Consulta MySQL Habitaciones Hotel

En primer lugar, muchas gracias por tu respuesta.
Solo me asalta una duda... ¿Realmente creas un registro en la tabla "tbl_fechas_habitaciones" por cada día reservado? ¿Es correcto?
SI es correcto.
Creo que ya lo soluciones, xD, si no, corrigeme:
Código:
SELECT
tbl_fechas_habitaciones.idFecha_Habitacion,
tbl_fechas_habitaciones.refHabitacion,
tbl_fechas_habitaciones.fechas,
tbl_hotel.strNombre,
tbl_hotel.intCategoria,
tbl_habitaciones_hotel.refTipo_Habitacion,
tbl_habitaciones_hotel.refImagenes,
tbl_habitaciones_hotel.intEstado,
tbl_habitaciones_hotel.intNumero_Habitacion,
tbl_habitaciones_hotel.refHotel,
tbl_habitaciones_hotel.id_Habitacion,
tbl_hotel.idHotel,
tbl_habitaciones_hotel.refUsuario,
tbl_habitaciones_hotel.intPrecio_Especial,
tbl_habitaciones_hotel.intPrecio_Alta,
tbl_habitaciones_hotel.intPrecio_Media,
tbl_habitaciones_hotel.intPrecio_Baja,
tbl_habitaciones_hotel.strDescuento,
tbl_habitaciones_hotel.intDescuento_Estado,
tbl_habitaciones_hotel.intPromocion,
tbl_habitaciones_hotel.strPromocion_Contenido,
tbl_habitaciones_hotel.strDescripcion,
tbl_habitaciones_hotel.intAdultos,
tbl_habitaciones_hotel.intNinos,
tbl_habitaciones_hotel.intBebes,
tbl_hotel.intEstado,
tbl_hotel.dblLatitud,
tbl_hotel.dblLongitud,
tbl_hotel.idProvincia,
tbl_hotel.strPoblacion,
tbl_hotel.strResumen,
tbl_hotel.intNumero_Habitaciones,
tbl_hotel.strEmail,
tbl_hotel.strTelefono
FROM
tbl_fechas_habitaciones
INNER JOIN tbl_habitaciones_hotel ON tbl_fechas_habitaciones.refHabitacion = tbl_habitaciones_hotel.id_Habitacion
INNER JOIN tbl_hotel ON tbl_habitaciones_hotel.refHotel = tbl_hotel.idHotel
WHERE
tbl_hotel.intEstado = 1 AND tbl_habitaciones_hotel.intEstado = 1 AND tbl_hotel.idProvincia = 43
AND tbl_habitaciones_hotel.id_Habitacion NOT IN(SELECT DISTINCT (tbl_fechas_habitaciones.refHabitacion) FROM tbl_fechas_habitaciones
WHERE tbl_fechas_habitaciones.fechas BETWEEN '2015-12-18' AND '2015-12-28')
GROUP BY tbl_fechas_habitaciones.refHabitacion
La consulta me arroja este resultado:

refHabitacion Fecha
1 2016-01-01
3 2016-01-05

La Habitacion id 1 tienes reservados desde el 2016-01-01 al 2016-01-07
La Habitacion id 3 tiene reservados desde el 2016-01-05 al 2016-01-16
La Habitacion id 2 tiene reservados desde el 2015-12-18 al 2015-12-31

¿¿ Nos vamos acercando o creo que ya está ??

Conclusión: es que el buscador me dice que hay dos habitaciones libres en las fechas seleccionadas la 1 y la 3 además la consulta me da las fechas que están ocupadas de esas dos Habitaciones. Que lio..........

Un saludo: Manyblue
  #9 (permalink)  
Antiguo 19/12/2015, 14:02
Avatar de manyblue  
Fecha de Ingreso: marzo-2008
Mensajes: 329
Antigüedad: 16 años
Puntos: 10
Respuesta: Consulta MySQL Habitaciones Hotel

Pues NO, la consulta anterior no va como desearía. He estado pensando más detenidamente sobre el tema y me he equivocado en el planteamiento. En la primera consulta me tengo que olvidar de la tbl_fechas_habitaciones por lo que la consulta al final me ha quedado así:
Código:
SELECT
tbl_habitaciones_hotel.id_Habitacion,
tbl_habitaciones_hotel.refHotel,
tbl_habitaciones_hotel.refImagenes,
tbl_habitaciones_hotel.refTipo_Habitacion,
tbl_habitaciones_hotel.refUsuario,
tbl_habitaciones_hotel.intNumero_Habitacion,
tbl_habitaciones_hotel.intEstado,
tbl_habitaciones_hotel.intAdultos,
tbl_habitaciones_hotel.intNinos,
tbl_habitaciones_hotel.intBebes,
tbl_habitaciones_hotel.intPrecio_Especial,
tbl_habitaciones_hotel.intPrecio_Alta,
tbl_habitaciones_hotel.intPrecio_Media,
tbl_habitaciones_hotel.intPrecio_Baja,
tbl_habitaciones_hotel.strDescuento,
tbl_habitaciones_hotel.intDescuento_Estado,
tbl_habitaciones_hotel.intPromocion,
tbl_habitaciones_hotel.strPromocion_Contenido,
tbl_habitaciones_hotel.Fecha_Alta_habitacion,
tbl_habitaciones_hotel.Fecha_Modificacion_Habitacion,
tbl_habitaciones_hotel.strDescripcion,
tbl_habitaciones_hotel.strDescripcion_ing,
tbl_habitaciones_hotel.strDescripcion_ale,
tbl_habitaciones_hotel.strDescripcion_fra,
tbl_habitaciones_hotel.strDescripcion_hol,
tbl_habitaciones_hotel.intTDT,
tbl_habitaciones_hotel.intTV_Sat,
tbl_habitaciones_hotel.intTelefono,
tbl_habitaciones_hotel.intInternet_Cable,
tbl_habitaciones_hotel.intWifi,
tbl_habitaciones_hotel.intNevera,
tbl_habitaciones_hotel.intCaja_Fuerte,
tbl_habitaciones_hotel.intBanera,
tbl_habitaciones_hotel.intJacuzzi,
tbl_habitaciones_hotel.intSecador_Pelo,
tbl_habitaciones_hotel.intAire_Acondicionado,
tbl_habitaciones_hotel.intMicroondas,
tbl_habitaciones_hotel.intCocina,
tbl_habitaciones_hotel.intLavadora,
tbl_habitaciones_hotel.intServicio_habitaciones,
tbl_habitaciones_hotel.intMenage_Cocina,
tbl_habitaciones_hotel.intCuna_Bebe,
tbl_habitaciones_hotel.intBalcon,
tbl_habitaciones_hotel.intInsonorizacion,
tbl_habitaciones_hotel.intPlancha_Tabla,
tbl_habitaciones_hotel.intClimatizacion,
tbl_habitaciones_hotel.intBide,
tbl_habitaciones_hotel.intHilo_musical,
tbl_habitaciones_hotel.intMaximo_Personas,
tbl_habitaciones_hotel.intVista_Mar,
tbl_habitaciones_hotel.intVista_Montana,
tbl_habitaciones_hotel.intVista_Ciudad,
tbl_habitaciones_hotel.intVista_Panoramica,
tbl_habitaciones_hotel.intAdmite_Mascotas,
tbl_hotel.idHotel,
tbl_hotel.refUsuario,
tbl_hotel.strNombre,
tbl_hotel.strNombreCEO,
tbl_hotel.strNombre_Empresa,
tbl_hotel.strNIF_CIF,
tbl_hotel.intEstado,
tbl_hotel.intCategoria,
tbl_hotel.strDireccion,
tbl_hotel.strPoblacion,
tbl_hotel.idProvincia,
tbl_hotel.strEstadoPaisExtrangero,
tbl_hotel.intCodigo_Postal,
tbl_hotel.strPais,
tbl_hotel.strtDescripcion,
tbl_hotel.strResumen,
tbl_hotel.strResumen_ing,
tbl_hotel.strResumen_ale,
tbl_hotel.strResumen_fra,
tbl_hotel.strResumen_hol,
tbl_hotel.strDescripcion_ing,
tbl_hotel.strDescripcion_ale,
tbl_hotel.strDescripcion_fra,
tbl_hotel.strDescripcion_hol,
tbl_hotel.dblLatitud,
tbl_hotel.dblLongitud,
tbl_hotel.intNumero_Habitaciones,
tbl_hotel.strEmail,
tbl_hotel.strTelefono,
tbl_hotel.strFax,
tbl_hotel.strMovil,
tbl_hotel.intWifi,
tbl_hotel.intRestaurante,
tbl_hotel.intCafeteria,
tbl_hotel.intGaraje,
tbl_hotel.intJardines,
tbl_hotel.strInstalaciones_Deportivas,
tbl_hotel.intPiscinaExterior,
tbl_hotel.intPiscinaInterior,
tbl_hotel.intAnimalesCompania,
tbl_hotel.intServicioDiscrecional,
tbl_hotel.intSpa,
tbl_hotel.Fecha_Alta_Hotel,
tbl_hotel.Fecha_Modificacion_Hotel
FROM
tbl_habitaciones_hotel
INNER JOIN tbl_hotel ON tbl_habitaciones_hotel.refHotel = tbl_hotel.idHotel
WHERE tbl_habitaciones_hotel.intEstado = 1 AND tbl_hotel.intEstado = 1 AND tbl_hotel.idProvincia = 43
AND  tbl_habitaciones_hotel.id_Habitacion NOT IN( SELECT DISTINCT  (tbl_fechas_habitaciones.refHabitacion) FROM tbl_fechas_habitaciones
WHERE
tbl_fechas_habitaciones.fechas BETWEEN '2015-12-18' AND '2015-12-25')
El resultado es correcto, solo hay la habitacion 3 del hotel 1 libre en esas fechas.
Gracias a todos por vuestra inestimable ayuda.
Un saludo: Manyblue

Etiquetas: bases-de-datos-general, hotel, select, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 01:31.