0

I just want to get the rows depending on where is the join. It is either HOSPEDAJE joining HOTEL, or HOSPEDAJE joining APARTAMENTO, but never at the same time.

I'm getting this error: #1241 - Operand should contain 1 column(s)

SELECT DISTINCT(Nombre,Ciudad,Provincia,Estrellas,Tipo,null,null) 
FROM hospedaje, hotel, habitacion
WHERE 
  hotel.hospedaje_id = hospedaje.id
  AND habitacion_id = habitacion.id

UNION

SELECT DISTINCT(Nombre,Ciudad,Provincia,null,null,Disponibles,Capacidad) 
FROM hospedaje, apartamento
WHERE apartamento.hospedaje_id = hospedaje.id
showdev
  • 28,454
  • 37
  • 55
  • 73
rmartrenado
  • 1,516
  • 1
  • 18
  • 42
  • 1
    possible duplicate of [MySQL error 1241: Operand should contain 1 column(s)](http://stackoverflow.com/questions/15820288/mysql-error-1241-operand-should-contain-1-columns) – showdev Feb 06 '14 at 18:10

1 Answers1

1

try

SELECT Nombre,Ciudad,Provincia,null as Estrellas,null as Tipo,Disponibles,Capacidad
FROM hospedaje, apartamento
WHERE apartamento.hospedaje_id = hospedaje.id

UNION

SELECT Nombre,Ciudad,Provincia,Estrellas,Tipo,null,null
FROM hospedaje, hotel, habitacion
WHERE 
  hotel.hospedaje_id = hospedaje.id
  AND habitacion_id = habitacion.id

ORDER BY 1

Union does a distinct anyway, you use union all otherwise.

the correct way to use distinct is without the (), for example

SELECT DISTINCT Nombre,Ciudad,Provincia,Estrellas,Tipo,null,null
FROM hospedaje, hotel, habitacion
WHERE 
  hotel.hospedaje_id = hospedaje.id
  AND habitacion_id = habitacion.id
Ian Kenney
  • 6,376
  • 1
  • 25
  • 44