有机会在 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;