CREATE TABLE IF NOT EXISTS `carslibrary` (
`CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
`CarName` varchar(255) NOT NULL,
PRIMARY KEY (`CarID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `colorslibrary` (
`ColorID` int(11) unsigned NOT NULL AUTO_INCREMENT,
`ColorName` varchar(255) NOT NULL,
PRIMARY KEY (`ColorID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
CREATE TABLE IF NOT EXISTS `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(11) unsigned NOT NULL,
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
我想选择carsLibrary的所有记录及其匹配车颜色,例如
carID carName carColor
1 toyota green
2 kia green
3 ford blue
答案 0 :(得分:1)
我使用了几个JOIN
s:
SELECT `CarID`, `CarName`, `ColorName` AS CarColor FROM
(`carslibrary` LEFT JOIN `facerecord` ON `CarID`=`carslibrary_id`)
LEFT JOIN `colorslibrary` ON `colorslibrary_id`=`ColorID`