任何機會在tbl_a有重複的ID?
查詢正確 - 檢查http://sqlfiddle.com/#!8/9c666/1
CREATE TABLE `tbl_a` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`b_id` int(11) NOT NULL,
`c_id` int(11) NOT NULL,
`xyz` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `tbl_b` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`abc` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ;
CREATE TABLE `tbl_c` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`def` varchar(30) NOT NULL,
PRIMARY KEY (`id`)
) ;
INSERT INTO `tbl_b` (`id`, `abc`) VALUES
(1, 'b1'),
(2, 'b2'),
(3, 'b3'),
(4, 'b4');
INSERT INTO `tbl_c` (`id`, `def`) VALUES
(1, 'c1'),
(2, 'c2'),
(3, 'c3');
INSERT INTO `tbl_a` (`b_id`, `c_id`, `xyz`)
SELECT (ABS(CRC32(UUID())) % 4) + 1, (ABS(CRC32(UUID())) % 3) + 1, SUBSTRING(UUID(), 3, 5);
SELECT * FROM tbl_a;
SELECT * FROM tbl_a
INNER JOIN tbl_b on tbl_a.b_id = tbl_b.id
INNER JOIN tbl_c on tbl_a.c_id = tbl_c.id;
我會仔細檢查我的數據,但我不這麼認爲... –
原來是一個數據錯誤。謝謝。 –