0

这是有效的 SQL 吗?as well的用法对GROUP BY吗?

另外,如何按 TOTAL 对结果进行排序?

谢谢

     SELECT packages.id          AS PPIIDD,
       Q_ALLOTMENTS.total   AS TOTAL,
       roomname,
       hotelrooms.room_id   AS RID,
       packages.description AS PDESC,
       hotelname,
       regionname,
       places.name          AS PLACENAME,
       destinationname,
       hotels.property_desc,
       hotels.hotel_id      AS HID,
       room_rates.high      AS ROOM_HIGH,
       packages.low         AS PLOW,
       hotels.startype,
       address1,
       address2,
       town,
       postcode,
       hotelrooms.room_id   AS RID,
       packages.type        AS PTYPE
FROM   packages
       INNER JOIN hotels
               ON hotels.hotel_id = packages.hotel_id
                  AND hotels.status = 'active'
       INNER JOIN allotments_new
               ON allotments_new.package_id = packages.id
                  AND allotments_new.day_date >= '2013-04-17'
                  AND allotments_new.day_date < '2013-04-18'
                  AND allotments_new.day_rate > 0
                  AND allotments_new.status != 'n'
                  AND allotments_new.minimum_number_of_nights <= 1
       INNER JOIN hotelrooms
               ON hotelrooms.room_id = packages.room_id
                  AND hotelrooms.status = 'active'
       INNER JOIN room_rates
               ON room_rates.room_id = hotelrooms.room_id
       INNER JOIN destinations
               ON destinations.destination_id = hotels.destination_id
       INNER JOIN regions
               ON regions.region_id = hotels.region_id
       LEFT OUTER JOIN places
                    ON places.id = hotels.places_id
       INNER JOIN (SELECT Sum(day_rate) AS TOTAL,
                          allotments_new.status,
                          allotments_new.number_of_rooms,
                          day_rate,
                          day_date,
                          package_id
                   FROM   allotments_new
                   WHERE  allotments_new.day_date >= '2013-04-17'
                          AND allotments_new.day_date < '2013-04-18'
                          AND allotments_new.day_rate > 0
                          AND allotments_new.status != 'n'
                          AND allotments_new.minimum_number_of_nights <= 1
                   GROUP  BY allotments_new.status,
                             allotments_new.number_of_rooms,
                             day_rate,
                             day_date,
                             package_id) AS Q_ALLOTMENTS
               ON Q_ALLOTMENTS.package_id = packages.id
WHERE  hotels.hotel_id = '1875'
       AND packages.status = 'active'
       AND hotels.status = 'active'
       AND hotelrooms.status = 'active'
       AND maximum_number_of_adults_and_children >= '1'
       AND 1 = '1'
       AND 1 = '1'
       AND 1 = '1'
       AND 1 = '1'
       AND 1 = '1'
       AND 1 = 1
       AND 1 = 1
GROUP  BY ppiidd
ORDER  BY startype DESC  
4

1 回答 1

3

Mysql 有一种特殊的方式来处理组,因为任何其他数据库都不支持 AFAIK。这里是:

通常,group by 必须列出所有未聚合的列。但是在 mysql 中,mysql 的行为不是抛出错误,而是为group by 子句列出的列的每个唯一组合返回随机选择的一行。出于实用性考虑,选择的行不是随机的,而是遇到的第一行。

答案是这不是错误,但查询可能不会返回您期望的结果。


顺便说一句,此功能对于根据列的某些子集获取第一行/最后一行非常方便,例如获取每个项目的最新行:

select * from (
    select * from mytable order by timestamp desc) x
group by item_id

中间查询确保以最新的时间戳顺序遇到行。时髦的 group by 返回每个 item_id 遇到的第一行,这当然是最新的。

于 2013-04-17T03:01:01.847 回答