BIG

싱글테이블 레코드 그룹 - 각 그룹별 마지막 레코드 가져오기

 

# 샘플 테이블 생성 ----------------------------------------------

CREATE TABLE `booking_11_default` (
  `b11_pk` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '고유번호',
  `b11_group_code` bigint(20) NOT NULL,
  `b11_code` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '주문코드/예약코드',
  `b11_status` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '상태',
  `fk_r01_pk` bigint(20) unsigned NOT NULL COMMENT '객실정보연결 외래키',
  `b11_start_time` bigint(20) unsigned DEFAULT NULL COMMENT 'unixtime - 입실예정시각',
  `b11_end_time` bigint(20) unsigned DEFAULT NULL COMMENT 'unixtime - 퇴실예정시각',
  `b11_orderer_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '주문자/예약자 이름',
  `b11_orderer_mobile` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '주문자/예약자 휴대전화',
  `b11_orderer_email` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '주문자/예약자 이메일',
  `b11_memo` text COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '기타정보',
  PRIMARY KEY (`b11_pk`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='booking_11_default 예약기본정보'

# SELECT Sample----------------------------------------------

SELECT * FROM booking_11_default AS A
         WHERE A.b11_pk=(
          SELECT MAX(B.b11_pk) FROM booking_11_default AS B
           GROUP BY B.b11_group_code
           HAVING A.b11_group_code=B.b11_group_code
         )
 ORDER BY b11_pk DESC
;
LIST