如何在mysql where子句中减去两列?

时间:2014-11-27 06:55:51

标签: mysql join

我有一个查询,我想减去两列。这是我的查询

SELECT 
    field_data_field_max_pax_allowed.`field_max_pax_allowed_value` AS m,
    field_data_field_bookable_item.`entity_id`,
    node.nid AS hotel_id, 
    field_data_field_rooms.`field_rooms_target_id` AS room, 
    field_data_field_quantity.`field_quantity_value` AS number_of_rooms,
    node.title,
    MAX(IFNULL(quan.`field_quantity_value`,0)) AS maxs,
    dr.`field_datetime_range_value`,
    dr.`field_datetime_range_value2`
FROM node node
LEFT JOIN field_data_field_rooms ON node.nid = field_data_field_rooms.entity_id 
INNER JOIN node n ON field_data_field_rooms.field_rooms_target_id = n.nid
LEFT JOIN field_data_field_max_pax_allowed ON field_data_field_rooms.`field_rooms_target_id` = field_data_field_max_pax_allowed.`entity_id`
LEFT JOIN field_data_field_quantity ON field_data_field_quantity.`entity_id` = field_data_field_rooms.`field_rooms_target_id`
LEFT JOIN field_data_field_bookable_item ON field_data_field_bookable_item.`field_bookable_item_target_id` = field_data_field_rooms.`field_rooms_target_id`
LEFT JOIN field_data_field_quantity quan ON field_data_field_bookable_item.`entity_id` = quan.`entity_id`
LEFT JOIN field_data_field_datetime_range dr ON field_data_field_bookable_item.`entity_id` = dr.`entity_id`
WHERE (node.type IN  ('hotel'))
    AND (DATE_FORMAT('2014-12-01', '%Y-%m-%d') BETWEEN DATE_FORMAT(dr.field_datetime_range_value, '%Y-%m-%d') AND DATE_FORMAT(dr.field_datetime_range_value2,'%Y-%m-%d')
        OR DATE_FORMAT('2014-12-31', '%Y-%m-%d') BETWEEN DATE_FORMAT(dr.field_datetime_range_value,'%Y-%m-%d') AND DATE_FORMAT(dr.field_datetime_range_value2,'%Y-%m-%d')
        OR DATE_FORMAT(dr.field_datetime_range_value, '%Y-%m-%d') BETWEEN DATE_FORMAT('2014-12-01', '%Y-%m-%d') AND DATE_FORMAT('2014-12-31', '%Y-%m-%d')
        OR DATE_FORMAT(dr.field_datetime_range_value2, '%Y-%m-%d') BETWEEN DATE_FORMAT('2014-12-31', '%Y-%m-%d') AND DATE_FORMAT('2014-12-01', '%Y-%m-%d')
        )
    AND (field_data_field_quantity.`field_quantity_value` - MAX(IFNULL(quan.`field_quantity_value`,0)) > 2)
GROUP BY field_data_field_rooms.`field_rooms_target_id`

显示错误代码:1111无效使用群组功能 我怎么解决呢?

1 个答案:

答案 0 :(得分:0)

WHERE子句用于选择要处理的行。你不能在那里汇总像MAX()这样的函数,因为在选择行之后才能计算出来 - 你有鸡与蛋的问题。

您应该将其放在HAVING子句中,该子句用于过滤生成的行。所以它应该是:

...
WHERE (node.type IN  ('hotel'))
    AND (DATE_FORMAT('2014-12-01', '%Y-%m-%d') BETWEEN DATE_FORMAT(dr.field_datetime_range_value, '%Y-%m-%d') AND DATE_FORMAT(dr.field_datetime_range_value2,'%Y-%m-%d')
        OR DATE_FORMAT('2014-12-31', '%Y-%m-%d') BETWEEN DATE_FORMAT(dr.field_datetime_range_value,'%Y-%m-%d') AND DATE_FORMAT(dr.field_datetime_range_value2,'%Y-%m-%d')
        OR DATE_FORMAT(dr.field_datetime_range_value, '%Y-%m-%d') BETWEEN DATE_FORMAT('2014-12-01', '%Y-%m-%d') AND DATE_FORMAT('2014-12-31', '%Y-%m-%d')
        OR DATE_FORMAT(dr.field_datetime_range_value2, '%Y-%m-%d') BETWEEN DATE_FORMAT('2014-12-31', '%Y-%m-%d') AND DATE_FORMAT('2014-12-01', '%Y-%m-%d')
        )
GROUP BY field_data_field_rooms.`field_rooms_target_id`
HAVING (field_data_field_quantity.`field_quantity_value` - MAX(IFNULL(quan.`field_quantity_value`,0)) > 2)