由于 MySql 不支持子查询中的主别名引用,因此创建具有多行的查询有点困难,您必须限制左连接表的行。
让我们概述一下简单的例子。
假设有两个表:
CREATE TABLE `items` (
`id` INT NOT NULL AUTO_INCREMENT,
`parent` VARCHAR(45) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
PRIMARY KEY (`id`));
CREATE TABLE `sub_items` (
`id` INT NOT NULL AUTO_INCREMENT,
`child` VARCHAR(45) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL,
`parent_id` INT NOT NULL,
PRIMARY KEY (`id`));
Run Code Online (Sandbox Code Playgroud)
内容如下:
INSERT INTO `items` (`parent`) VALUES ('Main item1');
INSERT INTO `items` (`parent`) VALUES ('Main item2');
INSERT INTO `items` (`parent`) VALUES ('Main item3');
INSERT INTO `sub_items` (`child`, `parent_id`) VALUES ('Child …Run Code Online (Sandbox Code Playgroud)