mysql中转置行到列的模糊错误

时间:2015-12-05 08:25:25

标签: mysql

tbl_attendance

CREATE TABLE `tbl_instructor_info` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `InstructorID` varchar(7) NOT NULL,
  `Name` varchar(50) NOT NULL,
  `Mobile` varchar(11) NOT NULL,
  `Email` varchar(50) NOT NULL,
  `Trade` int(1) NOT NULL,
  `TSP` int(1) NOT NULL,
  PRIMARY KEY (`ID`),
  UNIQUE KEY `InstructorID` (`InstructorID`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8

tbl_assigned

CREATE TABLE `tbl_assigned` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `Round` int(2) DEFAULT NULL,
  `TraineeID` varchar(7) DEFAULT NULL,
  `Name` varchar(25) DEFAULT NULL,
  `Mobile` int(10) DEFAULT NULL,
  `BatchID` varchar(35) DEFAULT NULL,
  `Remarks` varchar(8) DEFAULT NULL,
  `District` varchar(15) DEFAULT NULL,
  `Comments` varchar(21) DEFAULT NULL,
  `Level` varchar(2) DEFAULT NULL,
  `Trade` int(1) DEFAULT NULL,
  `Status` int(1) DEFAULT NULL,
  `Photo` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`ID`),
  KEY `TraineeID` (`TraineeID`)
) ENGINE=InnoDB AUTO_INCREMENT=356 DEFAULT CHARSET=utf8

tbl_instructor_info

CREATE TABLE `tbl_attendance` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `TraineeID` varchar(7) NOT NULL,
  `attnDate` date NOT NULL,
  `inTime` time NOT NULL,
  `outTime` time NOT NULL,
  `Status` varchar(10) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=316 DEFAULT CHARSET=latin1

TraineeID中出现含糊不清的错误。我为varchar使用了TraineeID类型数据。 Where子句也会产生错误。请求帮助我得到这个结果。

SET @sql = NULL;
SELECT
    GROUP_CONCAT(DISTINCT
    CONCAT('MAX(IF(`TraineeID`= ', `TraineeID`, ',"P","A")) AS ID',`TraineeID`)
            ) INTO @sql
    FROM tbl_attendance t1 JOIN tbl_assigned t2 
    ON t1.TraineeID=t2.TraineeID
    WHERE t2.BatchID='ID-Electronics/FMMTTC-01M/R7/01';  
    SET @sql = CONCAT('SELECT DATE_FORMAT(attnDate,"%d-%m-%Y")AS Date, ', @sql, ' 
    FROM tbl_attendance
    WHERE attnDate BETWEEN ',2015-12-01,' and ',2015-12-02,'
    GROUP BY attnDate');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

TraineeID中的tbl_attendance中的某些tbl_assigned不在TraineeID表中。实际上tbl_attendance中的tbl_assigned与两个数据表匹配。一个是tbl_Instructor_Info表,另一个是SET @sql = NULL; SELECT GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(`t1.TraineeID`= ', `t1.TraineeID` or `t3.InstructorID`, ',"P","A")) AS ID',`t1.TraineeID`) ) INTO @sql FROM tbl_attendance t1 JOIN tbl_assigned t2 ON t1.TraineeID=t2.TraineeID Join tbl_instructor_info t3 ON t1.TraineeID=t3.InstructorID WHERE t2.BatchID='ID-Electronics/FMMTTC-01M/R7/01'; SET @sql = CONCAT('SELECT DATE_FORMAT(attnDate,"%d-%m-%Y")AS Date, ', @sql, ' FROM tbl_attendance WHERE attnDate BETWEEN ',2015-12-01,' and ',2015-12-02,' GROUP BY attnDate'); PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; 。我应该使用如下:

SET @sql = NULL;
SELECT
    GROUP_CONCAT(DISTINCT
    CONCAT('MAX(IF(`TraineeID`= ', `TraineeID`, ',"P","A")) AS ID',`TraineeID`)
            ) INTO @sql
    FROM tbl_attendance;  

    SET @sql = CONCAT('SELECT DATE_FORMAT(attnDate,"%d-%m-%Y")AS Date, ', @sql, ' 
    FROM tbl_attendance
    WHERE attnDate BETWEEN ',2015-12-01,' and ',2015-12-02,'
    GROUP BY attnDate');

select @sql as aboutToUse;

-- PREPARE stmt FROM @sql;
-- EXECUTE stmt;
-- DEALLOCATE PREPARE stmt;

这是没有任何标准的转储; 查询:

TraineeID

转储数据     SELECT DATE_FORMAT(attnDate,"%d-%m-%Y")AS Date,MAX(IF(TraineeID = 22779," P"," A& #34;))AS ID22779,MAX(IF(TraineeID = 22780," P"," A"))AS ID22780,MAX(IF(TraineeID } = 24321," P"," A"))AS ID24321,MAX(IF(TraineeID = 24870," P",&#34) ; A"))AS ID24870,MAX(IF(TraineeID = 24871," P"," A"))AS ID24871,MAX(IF({{ 1}} = 25099," P"," A"))AS ID25099,MAX(IF(TraineeID = 25199," P",& #34; A"))AS ID25199,MAX(IF(TraineeID = 25304," P"," A"))AS ID25304,MAX(IF( TraineeID = 25308," P"," A"))AS ID25308,MAX(IF(TraineeID = 25515," P" ," A"))AS ID25515,MAX(IF(TraineeID = 25549," P"," A"))AS ID25549,MAX( IF(TraineeID = 25552," P"," A"))AS ID25552,MAX(IF(TraineeID = 25560," P&#) 34;," A"))AS ID25560,MAX(IF(TraineeID = 25571," P"," A"))AS ID25571, MAX(IF(TraineeID = 25592," P"," A"))AS ID25592,MAX(IF(TraineeID = 25594," P"," A"))AS ID25594,MAX(IF(TraineeID = 25597," P", " A"))AS ID25597,MAX(IF(TraineeID = 25600," P"," A"))AS ID25600,MAX(IF (TraineeID = 25710," P"," A"))AS ID25710,MAX(IF(TraineeID = 26105," P&#34) ;," A"))AS ID26105,MAX(IF(TraineeID = 26145," P"," A"))AS ID26145,MAX (IF(TraineeID = 26147," P"," A"))A         来自tbl_attendance         在2002年和2001年之间的关系         GROUP BY attnDate

1 个答案:

答案 0 :(得分:2)

在这一行

CONCAT('MAX(IF(`TraineeID`= ', `TraineeID`, ',"P","A")) AS ID',`TraineeID`)

您需要为t1指定哪个表别名(t2TraineeID

该列出现在两者中。

直到那时,

  

1052:字段列表中的“TraineeID”列不明确