我正在尝试运行此代码
INSERT INTO payroll_ho.tb_quotapos(QPCompany,QPQRndYear,QPQRndMonth,QPLine,QPPart,QPDep,QPBand,QPSubBand,QPPos,QPExist1,QPExist2,QPExist3)
SELECT "G02",2016,6,mp.PerWorkLine,mp.PerPart,mp.PerDep,PerBand,PerSubBand,mp.PerPos,c1,c2,c3 FROM ms_per mp
LEFT JOIN payroll_ho.tb_subbandpos ON BPBand=mp.PerSubBand
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c1 FROM ms_per WHERE PerQuota=1 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp1 ON mp.PerWorkLine=mp1.PerWorkLine AND mp.PerPart=mp1.PerPart AND mp.perdep=mp1.perdep AND mp.perpos=mp1.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c2 FROM ms_per WHERE PerQuota=2 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp2 ON mp.PerWorkLine=mp2.PerWorkLine AND mp.PerPart=mp2.PerPart AND mp.perdep=mp2.perdep AND mp.perpos=mp2.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c3 FROM ms_per WHERE PerQuota=3 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp3 ON mp.PerWorkLine=mp3.PerWorkLine AND mp.PerPart=mp3.PerPart AND mp.perdep=mp3.perdep AND mp.perpos=mp3.perpos
WHERE mp.PerEmploying="Y"
AND (c1>0 OR c2>0 OR c3>0)
GROUP BY mp.PerWorkLine,mp.PerPart,mp.PerDep,PerBand,PerSubBand,mp.PerPos
ON DUPLICATE KEY UPDATE QPExist1=c1, QPExist2=c2, QPExist3=c3
MySQL返回错误Unknown column 'c1' in 'field list'
为什么MySQL在c1
子句中没有看到SELECT
?
编辑:我认为问题出在ON DUPLICATE子句中。 SELECT工作正常。
EDIT2 :以下是每个表的定义。
CREATE TABLE `tb_quotapos` (
`QPCompany` varchar(3) NOT NULL,
`QPQRndYear` int(4) NOT NULL,
`QPQRndMonth` int(2) NOT NULL,
`QPLine` varchar(2) NOT NULL,
`QPPart` varchar(3) NOT NULL,
`QPDep` varchar(4) NOT NULL,
`QPBand` varchar(2) NOT NULL,
`QPSubBand` varchar(3) NOT NULL,
`QPPos` varchar(4) NOT NULL,
`QPJobDesc` varchar(15) DEFAULT NULL,
`QPQ1` int(3) DEFAULT NULL,
`QPQ2` int(3) DEFAULT NULL,
`QPQ3` int(3) DEFAULT NULL,
`QPExist1` int(3) DEFAULT NULL,
`QPExist2` int(3) DEFAULT NULL,
`QPExist3` int(3) DEFAULT NULL,
PRIMARY KEY (`QPCompany`,`QPQRndYear`,`QPQRndMonth`,`QPLine`,`QPPart`,`QPDep`,`QPBand`,`QPSubBand`,`QPPos`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
CREATE TABLE `ms_per` (
`PerCompany` varchar(3) NOT NULL COMMENT 'รหัสบริษัท/สาขา',
`PerCode` varchar(8) NOT NULL COMMENT 'รหัสพนักงาน',
`PerIdenNo` varchar(13) DEFAULT NULL COMMENT 'เลขประจำตัว',
`PerTitle` varchar(3) DEFAULT NULL COMMENT 'รหัสคำนำหน้า',
`PerFName` varchar(30) DEFAULT NULL COMMENT 'ชื่อ',
`PerLName` varchar(30) DEFAULT NULL COMMENT 'นามสกุล',
`PerEName` varchar(64) DEFAULT NULL COMMENT 'ชื่อภาษาอังกฤษ',
`PerCGroup` char(6) DEFAULT NULL COMMENT 'รหัสหน่วยงาน',
`PerWorkLine` char(2) DEFAULT NULL COMMENT 'รหัสสายงาน',
`PerPart` varchar(4) DEFAULT NULL COMMENT 'ฝ่าย',
`PerDep` varchar(4) DEFAULT NULL COMMENT 'รหัสแผนก',
`PerPos` varchar(4) DEFAULT NULL COMMENT 'รหัสตำแหน่ง',
`PerGroup` varchar(1) DEFAULT NULL COMMENT 'รหัสกลุ่มพนักงาน',
`PerEmploying` varchar(1) DEFAULT NULL COMMENT 'Y=อยู่ในบริษัท',
`PerTravel` char(1) DEFAULT NULL COMMENT 'รหัสการเดินทาง',
`PerTravCode` char(2) DEFAULT NULL COMMENT 'รหัสสายรถ',
`PerRoomNo` varchar(3) DEFAULT NULL COMMENT 'เลขห้องพัก',
`PerBand` char(2) DEFAULT '0' COMMENT 'รหัส Band พนักงาน',
`PerSubBand` char(3) DEFAULT '0' COMMENT 'รหัส Band พนักงาน',
`PerType` char(3) DEFAULT NULL COMMENT 'ประเภทพนักงาน',
`PerShift` char(1) DEFAULT NULL COMMENT 'ช่วงเวลาทำงาน (กะ)',
`PerShiftTime` char(3) DEFAULT '' COMMENT 'รหัส กะ(เวลา) เข้างาน (tb_workshift)',
`PerDuty` varchar(4) DEFAULT NULL COMMENT 'รหัสหน้าที่',
`PerDutyDesc` varchar(25) DEFAULT NULL,
`PerD_M` char(1) DEFAULT 'D' COMMENT 'D:รายวัน M:รายเดือน',
`PerQPay` int(1) DEFAULT '2' COMMENT 'จำนวนงวดจ่าย (ต่อเดือน)',
`PerTPay` char(1) DEFAULT '1' COMMENT 'จ่ายโดย 1:เข้าบัญชี 2:เงินสด',
`PerQHrPerDay` float(2,0) DEFAULT '8' COMMENT 'จำนวนชั่วโมงทำงาน/วัน (นำมาคำนวณค่าแรง/ช.ม.)',
`PerQDPerWeek` float(1,0) DEFAULT '6' COMMENT 'จำนวนวันทำงาน/สัปดาห์',
`PerSoFlag` char(1) DEFAULT 'Y' COMMENT 'Y=เคยยื่นแบบ สปส',
`PerSoStatus` char(1) DEFAULT 'Y' COMMENT 'Y:เข้าระบบประกันสังคม',
`PerForeign` char(1) DEFAULT NULL COMMENT 'Y:ต่างด้าว',
`BeginDate` varchar(6) DEFAULT NULL,
`PerBeginDate` date DEFAULT NULL COMMENT 'วันเริ่มงาน',
`BeginFull` varchar(8) DEFAULT NULL,
`FillDate` varchar(6) DEFAULT NULL,
`PerFillDate` date DEFAULT NULL COMMENT 'วันบรรจุงาน',
`EndDate` varchar(6) DEFAULT NULL,
`PerEndDate` date DEFAULT NULL COMMENT 'วันพ้นสภาพพนักงาน',
`PerEndCode` char(2) DEFAULT '-' COMMENT 'รหัสการออก/พ้นสภาพพนักงาน',
`PerEndLevel` char(2) DEFAULT '' COMMENT 'ระดับการพ้นสภาพ',
`PerQuota` char(1) DEFAULT NULL COMMENT 'โควต้า(อัตรากำลังคน) 1:ชาย 2:หญิง 3:ชายหรือหญิง',
`PerQuotaNo` varchar(10) DEFAULT NULL COMMENT 'เลขที่ใบขออัตรากำลัง',
`PerPictFile` varchar(50) DEFAULT NULL COMMENT 'ชื่อไฟล์รูปภาพ',
`PerLastComp` varchar(35) DEFAULT NULL COMMENT 'ที่ทำงานก่อนหน้านี้',
`PerSkill` char(1) DEFAULT NULL COMMENT 'รหัสฝีมือ',
`PerCutShirt` char(1) DEFAULT NULL COMMENT 'มีประกันเสื้อ',
`PerISO` char(1) DEFAULT 'Y' COMMENT 'Y=เข้าระบบ ISO N=ไม่เข้า',
`PerISOType` char(1) DEFAULT '0' COMMENT 'ประเภท ISO',
`PerTransfer` char(1) DEFAULT '-' COMMENT '9=โอนแล้ว',
`sPerIdenEDate` varchar(8) DEFAULT NULL,
`PerIdenEDate` date DEFAULT NULL COMMENT 'วันบัตรหมดอายุ',
`per_user` varchar(10) DEFAULT NULL,
`per_comid` varchar(15) DEFAULT NULL,
`per_update` datetime DEFAULT NULL,
PRIMARY KEY (`PerCompany`,`PerCode`),
KEY `PerIdenNo` (`PerIdenNo`) USING BTREE,
KEY `PerCode` (`PerCode`),
KEY `PerEmploying` (`PerEmploying`),
KEY `PerCGroup` (`PerCGroup`),
KEY `SrchQuota` (`PerWorkLine`,`PerPart`,`PerDep`,`PerPos`,`PerBand`,`PerSubBand`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='แฟ้มข้อมูลพนักงาน'
CREATE TABLE `tb_subbandpos` (
`BPCompany` varchar(3) NOT NULL COMMENT 'รหัสบริษัท',
`BPBand` varchar(2) NOT NULL COMMENT 'รหัสแบนด์',
`BPSubBand` varchar(3) NOT NULL COMMENT 'รหัสซับแบนด์',
`BPPosCode` varchar(4) NOT NULL COMMENT 'รหัสตำแหน่ง',
PRIMARY KEY (`BPCompany`,`BPBand`,`BPSubBand`,`BPPosCode`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
答案 0 :(得分:1)
尝试以下操作(注意,我仍在编辑它以重新获取数据库名称,我认为表名前面有2个点)。
但是通过涉及派生表来清理列名已经成功了。派生表具有将值折叠到包装它的查询中的奇妙副作用。在这种情况下,它会在INSERT ON DUPLICATE UPDATE上删除group by
禁止使用。
INSERT INTO tb_quotapos(QPCompany,QPQRndYear,QPQRndMonth,QPLine,QPPart,QPDep,QPBand,QPSubBand,QPPos,QPExist1,QPExist2,QPExist3)
select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12
from
( SELECT "G02" as col1,2016 as col2,6 as col3,
mp.PerWorkLine as col4,mp.PerPart as col5,mp.PerDep as col6,
PerBand as col7,PerSubBand as col8,mp.PerPos as col9,
c1 as col10,c2 as col11,c3 as col12
FROM ms_per mp
LEFT JOIN tb_subbandpos ON BPBand=mp.PerSubBand
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c1 FROM ms_per WHERE PerQuota=1 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp1 ON mp.PerWorkLine=mp1.PerWorkLine AND mp.PerPart=mp1.PerPart AND mp.perdep=mp1.perdep AND mp.perpos=mp1.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c2 FROM ms_per WHERE PerQuota=2 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp2 ON mp.PerWorkLine=mp2.PerWorkLine AND mp.PerPart=mp2.PerPart AND mp.perdep=mp2.perdep AND mp.perpos=mp2.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c3 FROM ms_per WHERE PerQuota=3 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp3 ON mp.PerWorkLine=mp3.PerWorkLine AND mp.PerPart=mp3.PerPart AND mp.perdep=mp3.perdep AND mp.perpos=mp3.perpos
WHERE mp.PerEmploying="Y"
AND (c1>0 OR c2>0 OR c3>0)
GROUP BY mp.PerWorkLine,mp.PerPart,mp.PerDep,PerBand,PerSubBand,mp.PerPos
) xDerived
ON DUPLICATE KEY UPDATE QPExist1=col10, QPExist2=col11, QPExist3=col12;
版本2(包含两个似乎需要payroll_ho.
的地方):
INSERT INTO payroll_ho.tb_quotapos(QPCompany,QPQRndYear,QPQRndMonth,QPLine,QPPart,QPDep,QPBand,QPSubBand,QPPos,QPExist1,QPExist2,QPExist3)
select col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11,col12
from
( SELECT "G02" as col1,2016 as col2,6 as col3,
mp.PerWorkLine as col4,mp.PerPart as col5,mp.PerDep as col6,
PerBand as col7,PerSubBand as col8,mp.PerPos as col9,
c1 as col10,c2 as col11,c3 as col12
FROM ms_per mp
LEFT JOIN payroll_ho.tb_subbandpos ON BPBand=mp.PerSubBand
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c1 FROM ms_per WHERE PerQuota=1 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp1 ON mp.PerWorkLine=mp1.PerWorkLine AND mp.PerPart=mp1.PerPart AND mp.perdep=mp1.perdep AND mp.perpos=mp1.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c2 FROM ms_per WHERE PerQuota=2 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp2 ON mp.PerWorkLine=mp2.PerWorkLine AND mp.PerPart=mp2.PerPart AND mp.perdep=mp2.perdep AND mp.perpos=mp2.perpos
LEFT JOIN (SELECT PerWorkLine,PerPart,PerDep,PerPos,count(*) c3 FROM ms_per WHERE PerQuota=3 AND PerEmploying="Y" AND PerISO="Y" AND PerCGroup = "01" GROUP BY PerWorkLine,PerPart,PerDep,PerPos) mp3 ON mp.PerWorkLine=mp3.PerWorkLine AND mp.PerPart=mp3.PerPart AND mp.perdep=mp3.perdep AND mp.perpos=mp3.perpos
WHERE mp.PerEmploying="Y"
AND (c1>0 OR c2>0 OR c3>0)
GROUP BY mp.PerWorkLine,mp.PerPart,mp.PerDep,PerBand,PerSubBand,mp.PerPos
) xDerived
ON DUPLICATE KEY UPDATE QPExist1=col10, QPExist2=col11, QPExist3=col12;
由于Group By
,派生表是必要的。来自手册页INSERT ... SELECT Syntax:
在ON DUPLICATE KEY UPDATE的值部分,您可以参考 其他表中的列,只要你不使用GROUP BY 选择部分。一个副作用是你必须限定nonunique列 值部分中的名称。