MySQL查询以不同的标准连接4个表

时间:2017-08-29 19:39:26

标签: mysql sql

我有4个表格(为了简洁而剥离到相关列):

CREATE TABLE `papers` (
   `paper_id` int(11) NOT NULL DEFAULT '0',
   PRIMARY KEY (`paper_id`)
);
INSERT INTO papers ( paper_id ) VALUES(1001);
INSERT INTO papers ( paper_id ) VALUES(1002);   
INSERT INTO papers ( paper_id ) VALUES(1003);
INSERT INTO papers ( paper_id ) VALUES(1004);
INSERT INTO papers ( paper_id ) VALUES(1005);
INSERT INTO papers ( paper_id ) VALUES(1006);

CREATE TABLE `questions` (
  `question_id` int(11) NOT NULL AUTO_INCREMENT,
  `type_id` int(11) NOT NULL,
  PRIMARY KEY (`question_id`)
);
INSERT INTO questions ( type_id ) VALUES(1);
INSERT INTO questions ( type_id ) VALUES(2);
INSERT INTO questions ( type_id ) VALUES(1);
INSERT INTO questions ( type_id ) VALUES(3);

CREATE TABLE `question_depends` (
  `question_id` int(11) NOT NULL,
  `depends_question_id` int(11) NOT NULL,
  `depends_answer_val` int(11) NOT NULL,
  PRIMARY KEY (`question_id`,`depends_question_id`)
);
INSERT INTO question_depends ( question_id, depends_question_id, depends_answer_val ) VALUES(3, 1, 0);
INSERT INTO question_depends ( question_id, depends_question_id, depends_answer_val ) VALUES(2, 1, 1);
INSERT INTO question_depends ( question_id, depends_question_id, depends_answer_val ) VALUES(3, 1, 1);

CREATE TABLE `answers` (
  `paper_id` int(11) NOT NULL,
  `question_id` int(11) NOT NULL,
  `answer_val` int(2) NOT NULL,
  PRIMARY KEY (`paper_id`,`question_id`)
); 
INSERT INTO answers ( paper_id, question_id, answer_val ) VALUES(1002, 1, 1);
INSERT INTO answers ( paper_id, question_id, answer_val ) VALUES(1002, 4, 0);
INSERT INTO answers ( paper_id, question_id, answer_val ) VALUES(1004, 1, 0);
INSERT INTO answers ( paper_id, question_id, answer_val ) VALUES(1004, 3, 1);
INSERT INTO answers ( paper_id, question_id, answer_val ) VALUES(1005, 1, 1);

我正在尝试提出一个查询,显示所有可能组合的所有数据:

  1. 所有论文ID应至少输出一次
  2. 给定的paper_id可能有也可能没有答案,可能有也可能没有依赖
  3. 最终目标是查看每个依赖性问题是否得到解答,如果是,如果答案值与每个纸质ID的依赖性答案值相匹配,并确定paper_id是否具有依赖性问题以及是否有任何问题未得到回答(是否有依赖)
  4. 我可以根据需要调整表格/数据
  5. 我接近:

    select P.paper_id as P_PID,
      A.paper_id as A_PID,
      A.question_id as A_QID,
      A.answer_val as A_VAL,
      QD.question_id as QD_QID,
      QD.depends_question_id AS QD_DQID,
      QD.depends_answer_val AS QD_VAL,
      Q.type_id AS Q_TYPE
    from papers P
    left join answers A on A.paper_id = P.paper_id
    left join question_depends QD on QD.depends_question_id = A.question_id
    left join questions Q on Q.question_id = QD.question_id
    UNION
    select NULL AS P_PID,
      NULL AS A_PID,
      A.question_id as A_QID,
      A.answer_val as A_VAL,
      QD.question_id as QD_QID,
      QD.depends_question_id AS QD_DQID,
      QD.depends_answer_val AS QD_VAL,
      Q.type_id AS Q_TYPE
    from question_depends QD
    left join answers A on QD.depends_question_id = A.question_id
    left join questions Q on Q.question_id = QD.question_id
      where A.question_id IS NULL 
    

    ...但是输出具有每个paper_id的每一行的答案数据,而不仅仅是该paper_id的答案数据。任何想法赞赏!使用这个小样本数据设置上面的选择输出:

    P_PID   A_PID   A_QID   A_VAL   QD_QID  QD_DQID QD_VAL  Q_TYPE
    1001                            
    1002    1002    1       1       2       1       1       2
    1002    1002    1       1       3       1       0       1
    1002    1002    4       0       NULL    NULL    NULL    NULL
    1003    NULL    NULL    NULL    NULL    NULL    NULL    NULL
    1004    1004    1       0       2       1       1       2
    1004    1004    1       0       3       1       0       1
    1004    1004    3       1       NULL    NULL    NULL    NULL
    1005    1005    1       1       2       1       1       2
    1005    1005    1       1       3       1       0       1
    1006    NULL    NULL    NULL    NULL    NULL    NULL    NULL
    

    理想的输出(如果我没有输入错误)将是:

    P_PID   A_PID   A_QID   A_VAL   QD_QID  QD_DQID QD_VAL  Q_TYPE
    1001    NULL    NULL    NULL    2       1       1       2
    1001    NULL    NULL    NULL    3       1       0       1
    1002    1002    1       1       2       1       1       2 
    1002    NULL    NULL    NULL    3       1       0       1
    1002    1002    4       0       NULL    NULL    NULL    3
    1003    NULL    NULL    NULL    2       1       1       2
    1003    NULL    NULL    NULL    3       1       0       1
    1004    1004    1       0       2       1       1       2 
    1004    NULL    NULL    NULL    3       1       0       1
    1004    1004    3       1       NULL    NULL    NULL    1
    1005    1005    1       1       2       1       1       2
    1005    NULL    NULL    NULL    3       1       0       1
    1006    NULL    NULL    NULL    2       1       1       2
    1006    NULL    NULL    NULL    3       1       0       1
    

1 个答案:

答案 0 :(得分:0)

“Divide et impera”。将问题分成三种情况。

else if (ModeButtonVMCaracteristiquesType == ModeButtonVMCaracteristiqueType.EDITIONCaracteristiqueTypeItem)
{
    int idCaracteristiqueSelected = Convert.ToInt32(CaracteristiqueSelected.idCharacteristicItem);

    var LineModified = (from x in ImItemsModel.imtypeitems select x.imcharacteristicsitems).ToList();
    LineModified.ForEach(p => ImItemsModel.Entry(p).State = System.Data.Entity.EntityState.Modified);
    var UpdateCaracteristicTypeItem = StaticGenericUpdate.UpadateRowInModel<imcharacteristicsitem>(ImItemsModel, "idCharacteristicItem", idCaracteristiqueSelected, "fk_idTypeItemIMCaracteristicsItems", fk_value, propertiesForModel, propertiesForView, this);

    ImItemsModel.SaveChanges();

您可以在SQL Fiddle http://sqlfiddle.com/#!9/fbd3a9/3

上查看结果