2014-02-06 27 views
0

我只想根据连接的位置获取行。它可以是HOSPEDAJE加入HOTELHOSPEDAJE加入APARTAMENTO,但从来没有在同一时间。无法构建该查询

我得到这个错误:#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 
+1

可能重复的[MySQL错误1241:操作数应包含1列(S)](http://stackoverflow.com/问题/ 15820288/mysql-error-1241-operand-should-contain-1-columns) – showdev

回答

1

尝试

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 ALL除外。

使用不同的正确的方法是不带(),例如

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

就像一个魅力我的朋友。非常感谢!!! – rmartrenado

+0

只是另一个问题......我如何排序Nombre的所有结果?非常感谢 – rmartrenado

+0

只需添加“order by” - 请参阅编辑答案 –