【问题标题】:Cannot make it to build this query无法构建此查询
【发布时间】:2014-03-03 20:32:23
【问题描述】:

我只想根据连接的位置获取行。要么是HOSPEDAJE 加入HOTEL,要么是HOSPEDAJE 加入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

【问题讨论】:

标签: mysql


【解决方案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 无论如何都是不同的,否则你使用 union all。

distinct的正确使用方式是不带(),例如

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

【讨论】:

  • 只是另一个问题...如何按 Nombre 对所有结果进行排序?非常感谢
猜你喜欢
  • 2016-02-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-29
  • 2021-10-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-09
  • 2017-05-10
相关资源
最近更新 更多