我的左联接查询有一个奇怪的问题:
表1:费率
Serial, peopleSerial, jobSerial
1, 23, 1
1, 24, 1
1, 25, 1
1, 26, 1
表2:人
Serial, otherKey, name1, address, etc...
23, 65, John, street, etc...
24, 66, Mike, street, etc...
25, 67, Alex, street, etc...
这对离开PEOPLE表中的数据来说应该很简单。 这是我正在使用的查询:
SELECT rates.*, people.* FROM rates
LEFT JOIN people ON people.Serial = Rates.peopleSerial
我希望看到这样的结果: 预期结果
Serial, peopleSerial, jobSerial, Serial, otherKey, name1, address, etc...
1, 23, 1, 23, 65, John, Street, etc...
2, 24, 1, 24, 66, Mike, Street, etc...
3, 25, 1, 25, 67, Alex, Street, etc...
4, 26, 1, NULL, NULL, NULL, NULL, etc...
但这实际上是我得到的结果: 实际结果
Serial, peopleSerial, jobSerial, Serial, otherKey, name1, address, etc...<br>
1, 23, 1, NULL, NULL, NULL, NULL, etc...
2, 24, 1, NULL, NULL, NULL, NULL, etc...
3, 25, 1, NULL, NULL, NULL, NULL, etc...
4, 26, 1, NULL, NULL, NULL, NULL, etc...
编辑:
这是相同的数据转储和SQL提琴,返回的结果相同。上面的示例是其简化版本。我已经清除了大部分subcon_people_dev表,因为它是机密信息,但仍应显示一些填充字段:
CREATE TABLE IF NOT EXISTS `subcon_rates` (
`serial` int(11) NOT NULL AUTO_INCREMENT,
`subcon_serial` int(11) DEFAULT NULL,
`job_serial` int(11) DEFAULT NULL,
`type_serial` int(11) DEFAULT NULL,
`rate` decimal(10,2) DEFAULT NULL,
`chargeRate` decimal(10,2) DEFAULT NULL,
`period` date DEFAULT NULL,
PRIMARY KEY (`serial`)
) ENGINE=MyISAM AUTO_INCREMENT=999 DEFAULT CHARSET=latin1;
INSERT INTO `subcon_rates` (`serial`, `subcon_serial`, `job_serial`, `type_serial`, `rate`, `chargeRate`, `period`) VALUES
(134, 52, 27, 1, 10.00, 15.00, '2018-10-14'),
(138, 56, 27, 1, 25.00, 30.00, '2018-10-14'),
(136, 55, 27, 1, 20.00, 25.00, '2018-10-14'),
(139, 54, 27, 1, 35.00, 40.00, '2018-10-14'),
(140, 52, 27, 1, 10.00, 15.00, '2018-10-07'),
(141, 56, 27, 1, 25.00, 30.00, '2018-10-07'),
(142, 55, 27, 1, 20.00, 25.00, '2018-10-07'),
(143, 54, 27, 1, 35.00, 40.00, '2018-10-07'),
(153, 54, 27, 1, 10.00, 15.00, '2018-10-21'),
(152, 55, 27, 1, 10.00, 15.00, '2018-10-21'),
(866, 52, 27, 1, 10.00, 15.00, '2018-10-21'),
(150, 52, 27, 1, 10.00, 15.00, '2018-10-21');
CREATE TABLE IF NOT EXISTS `subcon_people_dev` (
`serial` int(11) NOT NULL AUTO_INCREMENT,
`subconNumber` varchar(4) DEFAULT NULL,
`type` text,
`title` text,
`name1` text,
`surname` text,
`nino` text,
`ref` text,
`tradingname` text,
`crn` text,
`pName` text,
`pUTR` text,
`add1` text,
`add2` text,
`add3` text,
`add4` text,
`postcode` text,
`UTR` text,
`role` text,
`company` text,
`insuranceExpire` date DEFAULT NULL,
`approved` int(11) NOT NULL DEFAULT '0',
`sortCode` text,
`accountNumber` text,
`accountName` text,
`placcount` text,
PRIMARY KEY (`serial`),
UNIQUE KEY `subconNumber` (`subconNumber`)
) ENGINE=MyISAM AUTO_INCREMENT=69 DEFAULT CHARSET=latin1;
INSERT INTO `subcon_people_dev` (`serial`, `subconNumber`, `type`, `title`, `name1`, `surname`, `nino`, `ref`, `tradingname`, `crn`, `pName`, `pUTR`, `add1`, `add2`, `add3`, `add4`, `postcode`, `UTR`, `role`, `company`, `insuranceExpire`, `approved`, `sortCode`, `accountNumber`, `accountName`, `placcount`) VALUES
(1, '0001', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(2, '0010', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(3, '0011', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(4, '0012', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(5, '0013', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(6, '0014', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(7, '0015', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(8, '0016', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(9, '0017', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(10, '0018', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(11, '0019', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(12, '0002', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(13, '0020', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(14, '0021', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(15, '0022', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(16, '0023', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(17, '0024', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(18, '0025', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(19, '0026', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(20, '0027', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(21, '0028', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(22, '0029', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(23, '0003', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(24, '0030', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(25, '0031', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(26, '0032', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(27, '0033', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(28, '0034', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(29, '0035', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(30, '0036', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(31, '0037', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(32, '0038', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(33, '0039', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(34, '0004', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(35, '0040', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(36, '0041', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(37, '0042', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(38, '0043', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(39, '0044', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(40, '0045', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(41, '0046', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(42, '0047', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(43, '0048', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(44, '0049', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(45, '0005', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(46, '0050', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(47, '0051', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(48, '0052', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(49, '0006', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(50, '0007', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(51, '0008', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(59, '62', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(60, '54', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(61, '55', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(62, '59', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(63, '53', '2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, 0, '', '', '', ''),
(64, '58', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(65, '56', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(66, '60', '1', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(67, '0061', '1', '', '', '', '', '', '', '', NULL, NULL, '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', ''),
(68, '0063', '1', '', '', '', '', '', '', '', NULL, NULL, '', '', '', '', '', '', NULL, NULL, NULL, 0, '', '', '', '');
答案 0 :(得分:1)
因为没有任何匹配的键,所以得到空值...在subcon_people_dev中,您没有带有subcon_serial的行(52、54、55、56)。
答案 1 :(得分:0)
您需要更改加入顺序。对于Left Join
,最左边的表应该是您要考虑的所有行(无论其他表中是否有匹配的行)。
select subcon_rates.*, subcon_people_dev.*
from subcon_people_dev
left join subcon_rates on subcon_people_dev.`serial` = subcon_rates.subcon_serial