For this question, Question and Answer are just my example tables for one-many relationship. For example: Question(QuestionID, QuestionTitle, QuestionContent) and Answer(AnswerID, AnswerContent, QuestionID)
In one query, I want to get one question and all answers of this question by QuestionID:
How to write a query like that
In case that, I "think" will have each rows is an answer with additional fields of question. This will make duplication because all those answers have same question content --> cost memory to store additional data. So compare to this: get QuestionID and query get Question. get QuestionID and query to get all Answers. Does this way lost performance than first approach but gain more memory ?
Here is my script:
DROP TABLE IF EXISTS `Question` ;
CREATE TABLE IF NOT EXISTS `Question` (
`QuestionID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`QuestionContent` VARCHAR(200) NOT NULL,
PRIMARY KEY (`QuestionID`),
ENGINE = InnoDB;
DROP TABLE IF EXISTS `Answer` ;
CREATE TABLE IF NOT EXISTS `Answer` (
`AnswerID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`QuestionID` INT UNSIGNED NOT NULL,
`ReplyContent` VARCHAR(200) NOT NULL,
PRIMARY KEY (`AnswerID`),
INDEX `fk_Answer_Question1_idx` (`QuestionID` ASC),
CONSTRAINT `fk_Answer_Question1`
FOREIGN KEY (`QuestionID`)
REFERENCES `Question` (`QuestionID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ENGINE = InnoDB;
Thanks :)