开发者

mysql:Displaying of two rows in a single row

开发者 https://www.devze.com 2023-02-03 23:43 出处:网络
SELECT DATE_FORMAT(MAX(a.EffectiveDate),\'%d-%m-%Y\') AS `Date`, CONCAT(a.FirstNm,\' \',IFNULL(a.MiidleNM,\' \'),\' \',IFNULL(a.LastNM,\' \')) AS `EmployeeName`,
SELECT DATE_FORMAT(MAX(a.EffectiveDate),'%d-%m-%Y') AS `Date`,
       CONCAT(a.FirstNm,' ',IFNULL(a.MiidleNM,' '),' ',IFNULL(a.LastNM,' ')) AS `EmployeeName`, 
       `b`.`BranchName` AS `Branch`
  FROM `tbl_employeemaster` AS `a` 
  JOIN `tbl_branch` AS `b` ON a.BrnNm = b.idBranch     
UNION     
SELECT DATE_FORMAT(MAX(c.EffectiveDate),'%d-%m-%Y') AS `Date`,
       CONCAT(c.FirstNm,' ',IFNULL(c.MiidleNM,' '),' ',IFNULL(c.LastNM,' ')) AS `EmployeeName `, 
       `d`.`BranchName` AS `Branch`     
  FROM `tbl_employeehistory` AS `c`    
  JOIN `tbl_branch` AS `d` ON c.BrnNm = d.idBranch

...is the output:

Date        EmployeeName   Branch
-------------------------------------
15-01-2011  ABCD           Bangalore
10-10-2010  ABCD           Magalore

...but i want the output as shown below

EmployeeName    Branch      Date        开发者_StackOverflow社区previous Branch     Date
ABCD        Bangalore       15-01-2011  Mangalore       10-10-2010


Try this:

    SELECT a.EmployeeName,
           a.Branch,
           a.Date, b.branch previousBranch,
                   b.Date previousDate
    FROM
      ( SELECT DATE_FORMAT(MAX(a.EffectiveDate),'%d-%m-%Y') AS `Date`,
               CONCAT(a.FirstNm,' ',IFNULL(a.MiidleNM,' '),' ',IFNULL(a.LastNM,' ')) AS `EmployeeName`,
               `b`.`BranchName` AS `Branch`
       FROM `tbl_employeemaster` AS `a`
       INNER JOIN `tbl_branch` AS `b` ON a.BrnNm = b.idBranch) a
    LEFT JOIN
      (SELECT DATE_FORMAT(MAX(c.EffectiveDate),'%d-%m-%Y') AS `Date`,
              CONCAT(c.FirstNm,' ',IFNULL(c.MiidleNM,' '),' ',IFNULL(c.LastNM,' ')) AS `EmployeeName `,
              `d`.`BranchName` AS `Branch`
       FROM `tbl_employeehistory` AS `c`
       INNER JOIN `tbl_branch` AS `d` ON c.BrnNm = d.idBranch) b
   ON a.EmployeeName = b.EmployeeName
0

精彩评论

暂无评论...
验证码 换一张
取 消