我对MySQL并不十分熟悉,我正在从事一个项目,为了我的一生,我不明白为什么这个简单的join
会失败。从我的研究看来,通常的问题是缺乏参考约束。我认为我没有这个问题。
inner join
返回0行
left join
返回t1
中的所有行,但t2
的值为null
。
这是sql和表:
SELECT
t1.idOpportunities, t2.idEmploymentType, t2.Description
FROM
cnt4704p2.opportunities t1
INNER JOIN
cnt4704p2.employmenttype t2 ON t1.EmploymentTypeId = t2.idEmploymentType;
CREATE TABLE `opportunities` (
`idOpportunities` int(11) NOT NULL,
`Postion` varchar(150) DEFAULT NULL,
`EmploymentTypeId` int(11) DEFAULT NULL,
`PayRate` decimal(10,2) DEFAULT NULL,
`PayPeriodTypeId` int(11) DEFAULT NULL,
`idEmployers` int(11) DEFAULT NULL,
PRIMARY KEY (`idOpportunities`),
KEY `idEmployers_idx` (`idEmployers`),
KEY `idEmploymentTypeId_idx` (`EmploymentTypeId`),
KEY `fk_idPeriodType_idx` (`PayPeriodTypeId`),
CONSTRAINT `fk_idEmployers` FOREIGN KEY (`idEmployers`) REFERENCES `employers` (`idEmployers`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_idEmploymentTypeId` FOREIGN KEY (`EmploymentTypeId`) REFERENCES `employmenttype` (`idEmploymentType`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_idPeriodType` FOREIGN KEY (`PayPeriodTypeId`) REFERENCES `periodtypes` (`idPeriodTypes`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `employmenttype` (
`idEmploymentType` int(11) NOT NULL AUTO_INCREMENT,
`Description` varchar(45) DEFAULT NULL,
PRIMARY KEY (`idEmploymentType`),
KEY `idx_EmploymentTypeId` (`idEmploymentType`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
任何帮助将不胜感激。
按请求编辑:
SELECT
t1.idOpportunities, t2.idEmploymentType, t2.Description
FROM
cnt4704p2.opportunities t1
LEFT JOIN
cnt4704p2.employmenttype t2 ON t1.EmploymentTypeId = t2.idEmploymentType;
# idOpportunities, Postion, EmploymentTypeId, PayRate, PayPeriodTypeId, idEmployers
'1', 'ACCOUNTANT', '1', '32.00', '1', NULL
'2', 'Administrative Aide I', '2', '26.00', '1', NULL
'3', 'Assistant Public Works Director', '2', '82636.00', '2', NULL
'4', 'AWWU SYSTEMS ANALYST', '1', '42.00', '1', NULL
'5', 'BUSINESS ANALYST (Technology Analyst)', '1', '38.00', '1', NULL
'6', 'Code Compliance Officer I', '2', '32.00', '1', NULL
'7', 'CUSTOMER ENGINEERING SUPERVISOR', '1', '46.00', '1', NULL
'8', 'Desktop Support Technician', '2', '17.00', '1', NULL
'9', 'EQUIPMENT TECHNICIAN', '1', '34.00', '1', NULL
'10', 'Financial Analyst II', '2', '45.00', '1', NULL
'11', 'Fleet Mechanic', '2', '20.00', '1', NULL
'12', 'Heavy Equipment Operator(Streets)', '2', '47906.00', '2', NULL
'13', 'Inclusionary Aide', '4', '29.00', '1', NULL
'14', 'INDUSTRIAL PRETREATMENT INSPECTOR (Engineering)', '1', '35.00', '1', NULL
'15', 'JOURNEYMAN METERMAN', '1', '49.00', '1', NULL
'16', 'Jr. SQL Programmer', '2', '65000.00', '2', NULL
'17', 'LEAVE ADMINISTRATOR (Personnel Analyst)', '1', '29.00', '1', NULL
'18', 'Maintenance Mechanic', '2', '43387.00', '2', NULL
'19', 'ML&P Generation Division Manager', '1', '119725.00', '2', NULL
'20', 'ML&P UTILITY ACCOUNT REPRESENTATIVE II -...', '1', '18.00', '1', NULL
'21', 'Municipal Service Worker II', '3', '26.00', '1', NULL
'22', 'Network VOIP Admin', '2', '57500.00', '2', NULL
'23', 'NEW! BRANCH YOUTH SERVICES LIBRARIAN', '1', '28.00', '1', NULL
'24', 'Newtork Administrator', '2', '60000.00', '2', NULL
'25', 'PART-TIME LIBRARIAN', '6', '32.00', '1', NULL
'26', 'Police Officer (Certified)', '2', '46993.00', '2', NULL
'27', 'POLICE OFFICER (Patrol Officer Recruit)', '1', '43.00', '1', NULL
'28', 'POLICE OFFICER (Patrol Officer)', '1', '43.00', '1', NULL
'29', 'Police Officer Trainee', '2', '51250.00', '2', NULL
'30', 'PROCESS CONTROL TECHNICIAN', '1', '37.00', '1', NULL
'31', 'RECEPTIONIST (Office Associate)', '1', '21.00', '1', NULL
'32', 'Records Technician', '2', '39334.00', '2', NULL
'33', 'REGULATORY AFFAIRS ANALYST', '1', '42.00', '1', NULL
'34', 'SEASONAL GARDENER I (Gardener I)', '4', '19.00', '1', NULL
'35', 'SEASONAL PARKS CARETAKER I', '4', '19.00', '1', NULL
'36', 'SEASONAL PARKS CARETAKER I', '4', '19.00', '1', NULL
'37', 'SEASONAL RECREATION SPECIALIST I', '7', '18.00', '1', NULL
'38', 'Senior Elevator Inspector', '2', '43.00', '1', NULL
'39', 'Sr Programmer', '2', '108000.00', '2', NULL
'40', 'Stormwater Operator A', '2', '32.00', '1', NULL
'41', 'Stormwater Operator B', '2', '30.00', '1', NULL
'42', 'Stormwater Operator C', '2', '26.00', '1', NULL
'43', 'Stormwater Operator D', '2', '24.00', '1', NULL
'44', 'Systems Admin (VMWare)', '2', '65000.00', '2', NULL
'45', 'SYSTEMS ANALYST', '1', '42.00', '1', NULL
'46', 'Systems Analyst/Programmer 2', '2', '97008.00', '2', NULL
'47', 'Tier I/II Support Technician', '2', '40000.00', '2', NULL
'48', 'Traffic Engineer', '2', '42.00', '1', NULL
'49', 'TRANSIT PLANNING TECHNICIAN', '1', '29.00', '1', NULL
'50', 'Transportation Analyst PCA', '2', '43.00', '1', NULL
'51', 'Transportation Manager', '2', '35.00', '1', NULL
# idEmployerTypes, Description
'1', 'For-Profit Company'
'2', 'Government Agency'
'3', 'Non-Profit Company'
答案 0 :(得分:1)
如果join
的结果为0,则表示cnt4704p2.opportunities.EmploymentTypeId
中没有任何值与cnt4704p2.employmenttype.idEmploymentType
中的任何值匹配。
如果没有匹配的下标,很自然,left join
仅返回null
,因为这些列中没有匹配项。
检查这些列中的值及其数据类型。
答案 1 :(得分:-1)
好吧,我觉得自己是个白痴。谢谢SQLChao。我对正确的查找表进行了选择,似乎没有导入数据。因此,查询按预期执行的可能性很大。是的,只是插入了一些测试值,按预期工作。
很抱歉造成麻烦。这里已经很晚了。