CREATE PROCEDURE `spGetChat` ( IN `P_RoleTypeId` INTEGER , IN `P_UserId` INTEGER , IN `P_SourceId` INTEGER ) BEGIN SELECT `myMessages` . `FromUserId` AS `FromUserId` , `myMessages` . `FromRoleTypeId` AS `FromRoleTypeId` , `myMessages` . `FromSourceId` AS `FromSourceId` , `myMessages` . `ToRoleTypeId` AS `ToRoleTypeId` , `myMessages` . `ToSourceId` AS `ToSourceId` , `myMessages` . `ToUserId` AS `ToUserId` , `IFNULL` ( `groupList` . `lastMessage` , ? ) AS `CreateddateTime` , CASE WHEN `IFNULL` ( `myMessages` . `Chatdetail` , ? ) != ? THEN `myMessages` . `Chatdetail` ELSE `myMessages` . `ContentPath` END AS `Chatdetail` , ? AS `ChatGroupId` , `IFNULL` ( `U1` . `Name` , ? ) AS `ToUserName` , `IFNULL` ( `U2` . `Name` , ? ) AS `FromUserName` , ? AS `GroupTitle` FROM ( SELECT IF ( `Fromuserid` = `P_UserId` , `FromuserId` , `ToUserId` ) AS `FromUserId` , IF ( `FromRoleTypeId` = `P_RoleTypeId` , `FromRoleTypeId` , `ToRoleTypeId` ) AS `FromRoleTypeId` , IF ( `FromSourceId` = `P_SourceId` , `FromSourceId` , `ToSourceId` ) AS `FromSourceId` , IF 