gorm 子查詢

//實現代碼
//GetLatelyUserOperationFlow 獲取每個列表數據中的最近一條的未讀消息
func GetLatelyUserOperationFlow(orgId int32, objectId []int32) (userOperationFlowList []*TUserOperationFlow) {
    var subQuery = dao.InnerDao.Gorm.Table(TableUserOperationFlow(orgId)).Select("objectId objectId1 , max(createTime) createTime1 ").Where(" `status` = 2 and objectId in (?) ", objectId).Group("objectId").SubQuery()
    dao.InnerDao.Gorm.Table(TableUserOperationFlow(orgId)).Select(
        fmt.Sprintf("%s,%s,%s,%s,%s,%s,%s",
            TUserOperationFlowColumns.FlowID,
            TUserOperationFlowColumns.OperatorUserID,
            TUserOperationFlowColumns.EventType,
            TUserOperationFlowColumns.ObjectID,
            TUserOperationFlowColumns.ContentInfo,
            TUserOperationFlowColumns.CreateTime,
            TUserOperationFlowColumns.Status)).Joins(" join (?) as M on objectId = objectId1 and createTime = createTime1  ", subQuery).Scan(&userOperationFlowList)
    return userOperationFlowList
}

最終生成的sql

SELECT flowId,operatorUserId,eventType,objectId,contentInfo,createTime,status FROM  t_user_operation_flow_2817360   join ((SELECT objectId objectId1 , max(createTime) createTime1  FROM  t_user_operation_flow_2817360   WHERE ( `status` = 2 and objectId in (1,3) ) GROUP BY objectId)) as M on objectId = objectId1 and createTime = createTime1     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章