group by grouping queries Summary

1. After all the data field of the packet by a certain query conditions: as in the following according ReservePhone: the phone number of the packet, then the phone number of a specific query all data

select ID , GroupCode,HotelCode,AccountNo,ReservePhone,ReserveName,RoomNo,RoomType from Res_RoomResource 
where ID in (select (ID) from Res_RoomResource where GroupCode='0001' and hotelcode='021002' group by  ReservePhone,ID) 
and ReservePhone='18537*****';

下面这样查的话只能查询到一条记录
select ID , GroupCode,HotelCode,AccountNo,ReservePhone,ReserveName,RoomNo,RoomType from Res_RoomResource 
where ID in (select min(ID) from Res_RoomResource where GroupCode='0001' and hotelcode='021002' group by  ReservePhone,ID) 
and ReservePhone='18537693630';--min(ID) 或者Max(ID)

supplement:

1.对于表TA中存在主键id,所以可利用主键id来查询:

select ID , CODE , VALUE ,  DESC  , DATE form TA where ID in (select min(ID) from TA group by CODE , VALUE) ;

2.对于表TB中,表不存在主键,则查询方式为:

select min(ID) ID, CODE , VALUE ,  min(DESC) DESC ,min( DATE) DATE  from TA group by CODE , VALUE ;

总结:

方式1一般都适用 于单表的查询,因为一般建表的时候都会建主键;对于视图,联查的时候,方式一可能就不合适了,就可以用方式2

Guess you like

Origin www.cnblogs.com/newcapecjmc/p/12175648.html