首先,我想道歉,如果这是一个已知和回答的问题,但英语不是我的母语,我不知道如何搜索这样的问题。所以这里。
我有一张名为movie的桌子。此表包含有关电影的信息。电影的导演和作家也在我的商店中。但是导演和作家都是多元化的属性。作为多值,它们应该存储在新表中,但是那些表与电影表有很多关系,所以我创建了四个新表。
导演 - >指导 - >电影 作家 - >写道 - > MOVIE
[Director]和[writer]将名称lastanames和Id作为关键属性。 [指示],[写]从导演和作家表中保存id键和电影表中的键。
当我要求特定电影(有3位导演和2位作家)的导演和作家的名字时,我得到的结果就像::
D.Name D.Lastname W.Name W.Lastname
----------------------------------------------
Jan Kounen Stanley Kubric
Albert Hughes Stanley Kubric
Len Wiseman Stanley Kubric
Jan Kounen Alan Moore
Albert Hughes Alan Moore
Len Wiseman Alan Moore
如果我使用联合查询,我只能得到我想要的五个名字,但不能以某种方式理解谁是导演,谁是作家。
Jan Kounen
Albert Hughes
Len Wiseman
Stanley Kubric
Alan Moore
但都被列为董事。
所以我的问题是,我的设计有缺陷吗?我的查询错了吗?有没有办法让一个查询的导演和作者没有多个回复?
提前谢谢你。
EDIT ::::
谢谢大家的回复。
我试图为关系上传图片,但它不会让我。 所以这是有问题的表的代码
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`directed_by` (`Direc_id` INT(11) NOT NULL ,
`Db_Title` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`Direc_id`, `Db_Title`) ,
INDEX `fk_Director_has_Movie_Movie1_idx` (`Db_Title` ASC) ,
INDEX `fk_Director_has_Movie_Director1_idx` (`Direc_id` ASC) ,
CONSTRAINT `fk_Director_has_Movie_Director1`
FOREIGN KEY (`Direc_id` )
REFERENCES `Red_Apple_storeDB`.`director` (`D_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Director_has_Movie_Movie1`
FOREIGN KEY (`Db_Title` )
REFERENCES `Red_Apple_storeDB`.`movie` (`MV_Title` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`director` (`D_Name` VARCHAR(50) NOT NULL ,
`D_lastname` VARCHAR(45) NOT NULL ,
`D_id` INT(11) NOT NULL ,
PRIMARY KEY (`D_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`movie` (`MV_Title` VARCHAR(50) NOT NULL ,
`MV_Release_Date` DATE NOT NULL ,
`MV_Cost` BIGINT(20) NOT NULL ,
`St_Name` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`MV_Title`) ,
INDEX `fk_Movie_Studio1_idx` (`St_Name` ASC) ,
CONSTRAINT `fk_Movie_Studio1`
FOREIGN KEY (`St_Name` )
REFERENCES `Red_Apple_storeDB`.`studio` (`ST_Name` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`writen_by` (`Writ_id` VARCHAR(45) NOT NULL ,
`Wb_Title` VARCHAR(50) NOT NULL ,
PRIMARY KEY (`Writ_id`, `Wb_Title`) ,
INDEX `fk_Writer_has_movie_movie1_idx` (`Wb_Title` ASC) ,
INDEX `fk_Writer_has_movie_Writer1_idx` (`Writ_id` ASC) ,
CONSTRAINT `fk_Writer_has_movie_Writer1`
FOREIGN KEY (`Writ_id` )
REFERENCES `Red_Apple_storeDB`.`writer` (`W_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Writer_has_movie_movie1`
FOREIGN KEY (`Wb_Title` )
REFERENCES `Red_Apple_storeDB`.`movie` (`MV_Title` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
CREATE TABLE IF NOT EXISTS `Red_Apple_storeDB`.`writer` (`W_Name` VARCHAR(50) NOT NULL ,
`W_Lastname` VARCHAR(45) NULL DEFAULT NULL ,
`W_id` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`W_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
我要求的一部电影,有3位导演和2位作家。
我的查询::
select d_name, d_lastname,d_id
from director, movie,directed_by
where mv_title = 'from hell'
and mv_title = db_title
and direc_id = d_id
union all
select w_name, w_lastname,w_id
from movie, writer, writen_by
where mv_title = 'from hell'
and mv_title = wb_title
and writ_id = w_id
答案 0 :(得分:1)
在您的选择查询中添加一个名为Writer或Producer的列。
SELECT .... , 'Writer' AS Type
UNION ALL
SELECT .... , 'Producer' AS Type
当您向我提供完整的表格结构时,如果您愿意,我可以为您提供整个查询。