在mysql中如何将逗号分隔的列转换成多行?

ht4b089n  于 2021-06-20  发布在  Mysql
关注(0)|答案(0)|浏览(325)
  1. CREATE PROCEDURE `proc_GetTreeViewData_test`(IN WONO INT(11))
  2. BEGIN
  3. DROP TEMPORARY TABLE IF EXISTS tblWOTypeMapping;
  4. CREATE TEMPORARY TABLE tblWOTypeMapping (ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, CurrentWOTypeId int(11), ParrentWOTypeId int(11));
  5. INSERT INTO tblWOTypeMapping(CurrentWOTypeId, ParrentWOTypeId)VALUES(4,1);
  6. INSERT INTO tblWOTypeMapping(CurrentWOTypeId, ParrentWOTypeId)VALUES(1,2);
  7. INSERT INTO tblWOTypeMapping(CurrentWOTypeId, ParrentWOTypeId)VALUES(2,3);
  8. DROP TEMPORARY TABLE IF EXISTS tblTreeViewDetail;
  9. CREATE TEMPORARY TABLE tblTreeViewDetail (P3_WOTypeId int(11), P3_WOType varchar(20), WOID int(11), WONumber varchar(20), WOStatus int(11), ParentWONumber int(11), isLink int(10));
  10. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  11. SELECT p3_womaster.P3_WOTypeId, p3_wotype.P3_WOType, p3_womaster.P3_WOId, p3_womaster.P3_WONo, p3_womaster.P3_WOStatusId, p3_womaster.P3_WOParentWONo, 1 FROM p3_womaster
  12. JOIN p3_wotype on (p3_womaster.P3_WOTypeId = p3_wotype.P3_WOTypeId) WHERE P3_WONo = WONO;
  13. SET @P3_WOTypeId = (select P3_WOTypeId from p3_womaster where P3_WONO = WONO);
  14. SET @WONOTemp = WONO;
  15. While @P3_WOTypeId <> 3 DO
  16. SET @P3_WOParentWONO = (SELECT IFNull(P3_WOParentWONO,'') from p3_womaster where P3_WONO = @WONOTemp);
  17. IF (@P3_WOParentWONO IS NULL OR @P3_WOParentWONO = '') then
  18. SET @MappingCounter = (SELECT ID from tblWOTypeMapping where CurrentWOTypeId = @P3_WOTypeId);
  19. SET @MAXMappingCounter = (SELECT count(*) from tblWOTypeMapping);
  20. WHILE @MappingCounter < @MAXMappingCounter DO
  21. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  22. SELECT tblWOTypeMapping.ParrentWOTypeId, p3_wotype.P3_WOType, 0, 0, 0, 0,0
  23. from tblWOTypeMapping JOIN p3_wotype on (tblWOTypeMapping.ParrentWOTypeId = p3_wotype.P3_WOTypeId)
  24. WHERE tblWOTypeMapping.ID = @MappingCounter;
  25. SET @MappingCounter = @MappingCounter +1;
  26. END WHILE;
  27. SET @P3_WOTypeId = 3;
  28. ELSE
  29. SET @WONOTemp = @P3_WOParentWONO;
  30. SET @ExistFlag = (select count(P3_WOID) from p3_womaster where P3_WONO = @WONOTemp);
  31. IF(@ExistFlag is not null AND @ExistFlag > 0) then
  32. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  33. SELECT p3_womaster.P3_WOTypeId, p3_wotype.P3_WOType, p3_womaster.P3_WOID, p3_womaster.P3_WONo, p3_womaster.P3_WOStatusId, p3_womaster.P3_WOParentWONo, 1
  34. from p3_womaster JOIN p3_wotype on (p3_womaster.P3_WOTypeId = p3_wotype.P3_WOTypeId)
  35. WHERE p3_womaster.P3_WONO = @WONOTemp;
  36. ELSE
  37. SET @P3_WOTypeIdTemp = (SELECT ParrentWOTypeId from tblWOTypeMapping where CurrentWOTypeId = @P3_WOTypeId);
  38. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  39. SELECT @P3_WOTypeIdTemp, p3_wotype.P3_WOType, 0, @WONOTemp, 0, 0, 0
  40. from p3_wotype WHERE p3_wotype.P3_WOTypeId = @P3_WOTypeIdTemp;
  41. END IF;
  42. SET @P3_WOTypeId = (SELECT P3_WOTypeId from p3_womaster where P3_WONO = @WONOTemp);
  43. END IF;
  44. END WHILE;
  45. DROP TEMPORARY TABLE IF EXISTS TempTabletoStoreChildWO;
  46. CREATE TEMPORARY TABLE TempTabletoStoreChildWO (ID INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, P3_WOTypeId int(11), P3_WOId int(11), P3_WONo varchar(20), P3_StatusId int(11));
  47. SET @WONOTofindChild = WONO;
  48. INSERT into TempTabletoStoreChildWO (P3_WOTypeId, P3_WOId, P3_WONo, P3_StatusId)
  49. SELECT P3_WOTypeId, P3_WOId, P3_WONo, P3_WOStatusId from p3_womaster where P3_WOParentWONO = @WONOTofindChild;
  50. SET @TableMAXrow = (select count(*) from TempTabletoStoreChildWO);
  51. SET @TableCounter = 1;
  52. WHILE @TableCounter <= @TableMAXrow DO
  53. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  54. SELECT TempTabletoStoreChildWO.P3_WOTypeId, p3_wotype.P3_WOType, TempTabletoStoreChildWO.P3_WOId, TempTabletoStoreChildWO.P3_WONo, TempTabletoStoreChildWO.P3_StatusId, @WONOTofindChild,1
  55. from TempTabletoStoreChildWO JOIN p3_wotype on (TempTabletoStoreChildWO.P3_WOTypeId = p3_wotype.P3_WOTypeId) Where ID = @TableCounter;
  56. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  57. SELECT p3_womaster.P3_WOTypeId, p3_wotype.P3_WOType, p3_womaster.P3_WOId, p3_womaster.P3_WONo, p3_womaster.P3_WOStatusId, p3_womaster.P3_WOParentWONO,1
  58. FROM p3_womaster JOIN p3_wotype on (p3_womaster.P3_WOTypeId = p3_wotype.P3_WOTypeId) WHERE P3_WOParentWONO IN (
  59. SELECT P3_WONo from TempTabletoStoreChildWO Where ID = @TableCounter);
  60. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  61. SELECT p3_womaster.P3_WOTypeId, p3_wotype.P3_WOType, p3_womaster.P3_WOId, p3_womaster.P3_WONo, p3_womaster.P3_WOStatusId, p3_womaster.P3_WOParentWONO,1
  62. FROM p3_womaster JOIN p3_wotype on (p3_womaster.P3_WOTypeId = p3_wotype.P3_WOTypeId) WHERE P3_WOParentWONO IN (
  63. SELECT P3_WONo FROM p3_womaster where P3_WOParentWONO IN (SELECT P3_WONo from TempTabletoStoreChildWO Where ID = @TableCounter));
  64. INSERT INTO tblTreeViewDetail(P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus,ParentWONumber,isLink)
  65. SELECT p3_womaster.P3_WOTypeId, p3_wotype.P3_WOType, p3_womaster.P3_WOId, p3_womaster.P3_WONo, p3_womaster.P3_WOStatusId, p3_womaster.P3_WOParentWONO,1
  66. FROM p3_womaster JOIN p3_wotype on (p3_womaster.P3_WOTypeId = p3_wotype.P3_WOTypeId) WHERE P3_WOParentWONO IN (
  67. SELECT P3_WONo FROM p3_womaster where P3_WOParentWONO IN (SELECT P3_WONo FROM p3_womaster where P3_WOParentWONO IN (
  68. SELECT P3_WONo from TempTabletoStoreChildWO Where ID = @TableCounter)));
  69. SET @TableCounter = @TableCounter +1;
  70. END WHILE;
  71. SELECT tblTreeViewDetail.P3_WOTypeId, tblTreeViewDetail.P3_WOType, tblTreeViewDetail.WOID, tblTreeViewDetail.WONumber, tblTreeViewDetail.WOStatus,
  72. p3_wostatusmaster.P3_WOStatusDescription,
  73. tblTreeViewDetail.ParentWONumber,tblTreeViewDetail.isLink,
  74. (SELECT P3_CompletionPercentage from p3_processmaster where P3_ProcessId in
  75. (select P3_ProcessId from p3_woprocess where P3_WOProcessId in (SELECT MAX(P3_WOProcessId) from p3_woprocess WHERE P3_WOID = tblTreeViewDetail.WOID group by P3_WOID))) as CompletionPercentage,
  76. p3_womaster.P3_WOPlannedDeliveryDate
  77. from tblTreeViewDetail LEFT JOIN p3_womaster on (tblTreeViewDetail.WONumber = p3_womaster.P3_WONO)
  78. LEFT JOIN p3_wostatusmaster on (tblTreeViewDetail.WOStatus = p3_wostatusmaster.P3_WOStatusId)
  79. Where tblTreeViewDetail.P3_WOTypeId <> 3;
  80. DROP TEMPORARY TABLE IF EXISTS TempTabletoStoreChildWO;
  81. DROP TEMPORARY TABLE IF EXISTS tblTreeViewDetail;
  82. DROP TEMPORARY TABLE IF EXISTS tblWOTypeMapping;
  83. END

输出如下:

  1. # P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus, P3_WOStatusDescription, ParentWONumber, isLink, CompletionPercentage, P3_WOPlannedDeliveryDate
  2. '2', 'PWO', '5484', '121298982', '24', 'Yet To Start', '66656,121298981', '1', NULL, NULL
  3. '1', 'AWO', '5485', '121298983', '24', 'Yet To Start', '6754,121298982', '1', '0%', NULL
  4. '4', 'IO', '5486', '121298984', '24', 'Yet To Start', '121298983', '1', '0%', NULL

需要以下输出:

  1. # P3_WOTypeId, P3_WOType, WOID, WONumber, WOStatus, P3_WOStatusDescription, ParentWONumber, isLink, CompletionPercentage, P3_WOPlannedDeliveryDate
  2. '2', 'PWO', '5484', '121298982', '24', 'Yet To Start', '121298981', '1', NULL, NULL
  3. '2', 'PWO', '5484', '121298982', '24', 'Yet To Start', '66656', '1', NULL, NULL
  4. '1', 'AWO', '5485', '121298983', '24', 'Yet To Start', '121298982', '1', '0%', NULL
  5. '4', 'IO', '5486', '121298984', '24', 'Yet To Start', '121298983', '1', '0%', NULL
  6. '4', 'IO', '5486', '121298984', '24', 'Yet To Start', '6754', '1', '0%', NULL

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题