我创建了3个不同的表,编码就是这样
CREATE TABLE `shirt` ( `id` int(11) not null,`name` varchar(32),PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `shirt` (`id`,`name`) VALUES ('1','vneck'),('2','scoop neck'); CREATE TABLE `shirt_size` ( `shirtId` int(11) not null,`sizeId` int(11) not null,PRIMARY KEY (`shirtId`,`sizeId`),KEY `sizeId` (`sizeId`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `shirt_size` (`shirtId`,`sizeId`) VALUES ('1','2'),('1','3'),'4'),'5'),'1'),'6'),'7'); CREATE TABLE `size` ( `id` int(11) not null,`name` varchar(4),PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `size` (`id`,'xs'),'s'),('3','m'),('4','l'),('5','1x'),('6','2x'),('7','3x');
我正在用它来查询它
SELECT shirt.name,size.name FROM shirt INNER JOIN shirt_size ON shirt_size.shirtId = shirt.id INNER JOIN size ON size.id = shirt_size.sizeId
但是结果表只显示了衬衫的名称,我需要尺寸栏也显示在屏幕上.在FROM部分我穿衬衫,尺码但是出错了.在进一步观察它时,我看到很多人只将第一个表名放在FROM部分.我不知道如何表示size.name列.我究竟做错了什么?
它们具有相同的列名称(尽管来自不同的表).您需要在其中一列(或两列)上提供ALIAS,例如
原文链接:https://www.f2er.com/php/137832.htmlSELECT shirt.name as ShirtName,size.name as SizeName FROM shirt INNER JOIN shirt_size ON shirt_size.shirtId = shirt.id INNER JOIN size ON size.id = shirt_size.sizeId