如何为每个冠军选择当前的持有人?
|
我想在
championships
表中选择每个冠军的当前持有人,并为尚未有任何赢家的冠军返回NULL
。
这是两个表的create语句:
CREATE TABLE `championships` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`friendly_name` varchar(255) NOT NULL,
`rank` int(2) unsigned NOT NULL DEFAULT \'1\',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`),
UNIQUE KEY `friendly_name` (`friendly_name`)
) ENGINE=InnoDB;
CREATE TABLE `title_history` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`championship` int(10) unsigned NOT NULL,
`winner` varchar(255) NOT NULL,
`date_from` date NOT NULL,
`location` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
KEY `championship` (`championship`)
) ENGINE=InnoDB;
ALTER TABLE `title_history` ADD CONSTRAINT `title_history_ibfk_1` FOREIGN KEY (`championship`) REFERENCES `championships` (`id`) ON UPDATE CASCADE;
哪种MySQL语句将返回我想要的数据集?
没有找到相关结果
已邀请:
1 个回复
古擅坛犯
编辑版本: 进一步了解您的表格并从您的评论中尝试以下子选择:
如果我正确理解您的结构,那应该赢得每个冠军的最后一个获胜者吗?