如下有2张表 bugs 和 profiles , 其中 bugs 中的 assigned_to 和 reporter 外键指向 profiles 表的 userid 主键:
CREATE TABLE `bugs` (
`bug_id` mediumint(9) NOT NULL AUTO_INCREMENT,
`assigned_to` mediumint(9) NOT NULL,
`short_desc` varchar(255) NOT NULL,
`reporter` mediumint(9) NOT NULL,
PRIMARY KEY (`bug_id`),
KEY `bugs_assigned_to_idx` (`assigned_to`),
KEY `bugs_reporter_idx` (`reporter`),
CONSTRAINT `fk_bugs_assigned_to_profiles_userid` FOREIGN KEY (`assigned_to`) REFERENCES `profiles` (`userid`) ON UPDATE CASCADE,
CONSTRAINT `fk_bugs_reporter_profiles_userid` FOREIGN KEY (`reporter`) REFERENCES `profiles` (`userid`) ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=570183 DEFAULT CHARSET=utf8;
CREATE TABLE `profiles` (
`userid` mediumint(9) NOT NULL AUTO_INCREMENT,
`login_name` varchar(255) NOT NULL,
PRIMARY KEY (`userid`),
UNIQUE KEY `profiles_login_name_idx` (`login_name`),
) ENGINE=InnoDB AUTO_INCREMENT=11226 DEFAULT CHARSET=utf8;
本人现在想写一个select语句,获取指定 bug_id 的记录对应的 bug_id , assigned_to , short_desc ,reporter 值,但是assigned_to和reporter想取profiles表中对应 bug_id的用户全名,本人应该怎么写的?
本人这样写的话可以获取assigned_to对应的用户名
select t1.bug_id,t1.short_desc,t2.login_name from bugs t1 , profiles t2 where t1.bug_id = xxxxxx and t1.assigned_to=t2.userid
假如本人要同时获取 assigned_to 和 reporter 对应的用户名应该怎么写啊?
40
SELECT t1.bug_id , t1.short_desc , t2.login_name, t3.login_name AS reporter_login_name FROM bugs t1 , profiles t2, profiles t3 WHERE t1.bug_id = xxxxxx AND t1.assigned_to = t2.userid AND t1.reporter=t3.userid;