Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在下面编写SQL以显示所有详细信息_Mysql_Sql_Database - Fatal编程技术网

Mysql 如何在下面编写SQL以显示所有详细信息

Mysql 如何在下面编写SQL以显示所有详细信息,mysql,sql,database,Mysql,Sql,Database,我不知道我在使用sql时是否正确地使用了下面的方法,但这里是它 我想显示所有考试详细信息(考试详细信息(也称为会话)、考试中的问题、答案、惩罚、图像、视频等),但我尝试这样做的方式是一个大查询,但它无法工作,因为没有显示行 下面是我想为一次考试(并且只有一次考试)显示的内容: 考试详情 考试中的所有问题 每个问题的所有答案,并标记每个答案的价值 惩罚(查看是否启用) 错误答案的扣分 有问题的图像 问题视频 问题中的音频 模块详细信息 学生 下面显示了上面每个部分从数据库中需要的字段: 考试

我不知道我在使用sql时是否正确地使用了下面的方法,但这里是它

我想显示所有考试详细信息(考试详细信息(也称为会话)、考试中的问题、答案、惩罚、图像、视频等),但我尝试这样做的方式是一个大查询,但它无法工作,因为没有显示行

下面是我想为一次考试(并且只有一次考试)显示的内容:

  • 考试详情
  • 考试中的所有问题
  • 每个问题的所有答案,并标记每个答案的价值
  • 惩罚(查看是否启用)
  • 错误答案的扣分
  • 有问题的图像
  • 问题视频
  • 问题中的音频
  • 模块详细信息
  • 学生
下面显示了上面每个部分从数据库中需要的字段:

  • 考试详情

    SessionId、SessionName、SessionDuration、TotalMarks、SessionWeight

  • 考试中的所有问题

    QuestionId、QuestionNo、QuestionContent、NoofAnswers、问号、OptionId、OptionType、ReplyId、ReplyType

  • 每个问题的所有答案,并标记每个答案的价值

AnswerId,Answer,AnswerMarks

  • 惩罚(查看是否启用)
PenaltyEnabled

  • 处罚答复
PenaltyAnswerId、PenaltyAnswer、PenaltyMarks

  • 有问题的图像
ImageId,ImageFile

  • 问题视频
VideoId视频文件

  • 问题中的音频
AudioId,AudioFile

  • 模块详细信息
ModuleId、ModuleNo、ModuleName

  • 学生
StudentId

我的问题是,为了显示所有这些数据,应该如何编写SQL代码?我需要一个大的查询还是很多小的查询?我的尝试是在问题的底部,但下面是数据库表,其中显示了处理一次考试的详细信息及其所有详细信息

表格:

AnswerId (PK) Answer  QuestionId (FK)
1              A       4
2              C       5
3              A       6
4              B       6
5              B       7
6              D       7
7              E       7
8              G       8
学生

StudentId (PK)  StudentForename  StudentSurname
1              James            Parker
学生会议

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
会话

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
模块

ModuleId (PK)  ModuleNo  ModuleName
1              CHI2513   ICT
OptionId (PK)  OptionType
1               A-C        
2               A-D
3               A-E
4               A-F
5               A-G
ImageId (PK)  ImageFile
1              ImageFile/Tulips.png
2              ImageFile/Daisys.png
问题

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
回答:

AnswerId (PK) Answer  QuestionId (FK)
1              A       4
2              C       5
3              A       6
4              B       6
5              B       7
6              D       7
7              E       7
8              G       8
个人答案

IndividualId (PK)  AnswerId (FK) AnswerMarks  
1                    1              3       
2                    2              5       
3                    3              3       
4                    4              3       
5                    5              2       
6                    6              2       
7                    7              3       
8                    8              9       
处罚

PenaltyId(PK) SessionId (FK)  PenaltyEnalbed
1             1               1
ReplyId (PK)  ReplyType
1               Single
2               Multiple
处罚标志

PenaltyAnswerId (PK) PenaltyAnswer PenaltyMarks QuestionId (FK)
1                     B            1            4
2                     C            1            4
3                     A            1            5
4                     B            1            5
5                     D            1            5
6                     C            2            6
7                     D            2            6
8                     A            1            7
9                     C            1            7
10                    F            1            7
11                    G            1            7
12                    A            0            8
13                    B            0            8
14                    C            1            8
15                    D            1            8
16                    E            1            8
17                    F            0            8
回复

PenaltyId(PK) SessionId (FK)  PenaltyEnalbed
1             1               1
ReplyId (PK)  ReplyType
1               Single
2               Multiple
选项表

ModuleId (PK)  ModuleNo  ModuleName
1              CHI2513   ICT
OptionId (PK)  OptionType
1               A-C        
2               A-D
3               A-E
4               A-F
5               A-G
ImageId (PK)  ImageFile
1              ImageFile/Tulips.png
2              ImageFile/Daisys.png
图像

ModuleId (PK)  ModuleNo  ModuleName
1              CHI2513   ICT
OptionId (PK)  OptionType
1               A-C        
2               A-D
3               A-E
4               A-F
5               A-G
ImageId (PK)  ImageFile
1              ImageFile/Tulips.png
2              ImageFile/Daisys.png
视频

VideoId (PK) VideoFile
1              VideoFile/Speech.png
AudioId (PK) AudioFile
1               AudioFile/Song.png
音频

VideoId (PK) VideoFile
1              VideoFile/Speech.png
AudioId (PK) AudioFile
1               AudioFile/Song.png
图像\u问题

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
视频问题

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
音频问题

SessionId (FK)  StudentId (FK)
1              1
SessionId (PK) SessionName  SessionDuration  TotalMarks  SessionWeight  ModuleId (FK)
1             AAA          01:00:00        30         20            1
SessionId FK) QuestionId (PK)  QuestionNo QuestionContent NoofAnswers QuestionMarks OptionId (FK) ReplyId (FK)
1             4                1           Question 1      1           5              1            1
1             5                2           Question 2      1           3              2            1
1             6                3           Question 3      2           6              2            2
1             7                4           Question 4      3           7              5            2
1             8                5           Question 5      1           9              5            1
   ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo (FK)
   1                    1             1                2
VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                   4
AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo (FK)
1                      1            1                  5
以下是我失败的尝试:

SELECT s.SessionId, 
       SessionName, 
       SessionDuration, 
       TotalMarks, 
       SessionWeight, 
       q.QuestionId, 
       q.QuestionNo, 
       QuestionContent, 
       QuestionMarks, 
       q.OptionId, 
       OptionType, 
       q.ReplyId, 
       ReplyType, 
       a.AnswerId, 
       Answer, 
       NoofAnswers, 
       AnswerMarks, 
       PenaltyEnabled, 
       PenaltyAnswerId, 
       PenaltyAnswer, 
       PenaltyMarks, 
       i.ImageId, 
       au.AudioId, 
       v.VideoId, 
       ImageFile, 
       AudioFile, 
       VideoFile, 
       s.ModuleId, 
       ModuleNo, 
       ModuleName, 
       ss.StudentId 
FROM   Student st 
       INNER JOIN Student_Session ss 
               ON st.StudentId = ss.StudentId 
       INNER JOIN Session s 
               ON ss.SessionId = s.SessionId 
       INNER JOIN Question q 
               ON s.SessionId = q.SessionId 
       INNER JOIN Answer a 
               ON q.QuestionId = a.AnswerId 
       INNER JOIN Individual_Answer ia 
               ON a.AnswerId = ia.AnswerId 
       LEFT JOIN Module m 
              ON s.ModuleId = m.ModuleId 
       LEFT JOIN Penalty p 
              ON q.SessionId = p.SessionId 
       LEFT JOIN Option_Table o 
              ON q.OptionId = o.OptionId 
       LEFT JOIN Reply r 
              ON q.ReplyId = r.ReplyId 
       LEFT JOIN Penalty_Marks pm 
              ON q.QuestionId = pm.QuestionId 
       LEFT JOIN Image_Question iq 
              ON q.QuestionId = iq.QuestionNo 
       INNER JOIN Image i 
               ON iq.ImageId = i.ImageId 
       LEFT JOIN Audio_Question aq 
              ON q.QuestionId = aq.QuestionNo 
       INNER JOIN Audio au 
               ON aq.AudioId = au.AudioId 
       LEFT JOIN Video_Question vq 
              ON q.QuestionId = vq.QuestionNo 
       INNER JOIN Video v 
               ON vq.VideoId = v.VideoId 
WHERE  s.SessionId = 1
ORDER  BY q.QuestionId 
更新:

提交问题后,下面的代码能否将问题ID插入图像_问题表中:(这是我在SO上找到的,但已更新为我的表设计)

逻辑是这样的:

  • 用户请求问题表单。服务器设置令牌并包括对用户的响应
  • 用户上载包含令牌的图片。图像存储在临时表中
  • 步骤2重复n次
  • 如果用户提交带有标记值的问题,则在问题表中放置一个条目,并分配一个id。TempImage表中共享令牌的所有图像都将以现在已知的QuestionId插入到图像表中。然后删除ImageQuestionToken条目,并级联删除TempImage中的临时图像
  • 否则用户不提交问题,则删除文件并删除ImageQuestionToken条目

  • 关键问题:通常,使用外部联接表的内部联接不会返回任何行

    我使用microsoft SQL语法进行了子查询。希望能有帮助。请参阅底部的“我的警告”,如果愿意,可以忽略create procedure语句,而只需声明并在顶部将@SessionID设置为1

    create procedure GetStuff(
        @SessionID as integer
        ) as
    
    declare @StudentID int -- you may need to use a different data type
    select @StudentID = StudentID from Student_Session where SessionID = @SessionID
    
    -- now we can at least remove the sessions table from the join list.
    
    SELECT s.SessionId, 
           SessionName, 
           SessionDuration, 
           TotalMarks, 
           SessionWeight, 
           q.QuestionId, 
           q.QuestionNo, 
           QuestionContent, 
           QuestionMarks, 
           q.OptionId, 
           OptionType, 
           q.ReplyId, 
           ReplyType, 
           a.AnswerId, 
           Answer, 
           NoofAnswers, 
           AnswerMarks, 
           PenaltyEnabled, 
           PenaltyAnswerId, 
           PenaltyAnswer, 
           PenaltyMarks, 
           i.ImageId, 
           au.AudioId, 
           v.VideoId, 
           ImageFile, 
           AudioFile, 
           VideoFile, 
           s.ModuleId, 
           ModuleNo, 
           ModuleName, 
           ss.StudentId 
    FROM   Student st 
           INNER JOIN Student_Session ss 
                   ON st.StudentId = ss.StudentId 
           INNER JOIN Session s 
                   ON ss.SessionId = s.SessionId 
           INNER JOIN Question q 
                   ON s.SessionId = q.SessionId 
           INNER JOIN Answer a 
                   ON q.QuestionId = a.AnswerId 
           INNER JOIN Individual_Answer ia 
                   ON a.AnswerId = ia.AnswerId 
           LEFT outer JOIN Module m 
                  ON s.ModuleId = m.ModuleId 
           LEFT outer JOIN Penalty p 
                  ON q.SessionId = p.SessionId 
           LEFT outer JOIN Option_Table o 
                  ON q.OptionId = o.OptionId 
           LEFT outer JOIN Reply r
                  ON q.ReplyId = r.ReplyId 
           LEFT outer JOIN Penalty_Marks pm 
                  ON q.QuestionId = pm.QuestionId 
            -- you can't inner join off of an outer joined table, or you'll get no rows for the entire query. I've nested the query, which is bad practice but... practical for getting this monster of a query working.
           LEFT outer JOIN (select c3.ImageID, c3.ImageFile from Image_Question as a1 inner join Images as b1 on a1.QuestionNo = b1.QuestionNo inner join Image as c1 on a1.ImageID = c1.ImageID)
                   ON q.QuestionID = iq.QuestionID
            -- you can't inner join off of an outer joined table, or you'll get no rows for the entire query. I've nested the query, which is bad practice but... practical for getting this monster of a query working.
           LEFT outer JOIN (select b2.AudioID, b2.AudioFile from Audio_Question as a2 inner join Audio as b2 on a2.AudioID = b2.AudioID) as aq
                  ON q.QuestionId = aq.QuestionNo 
            -- you can't inner join off of an outer joined table, or you'll get no rows for the entire query. I've nested the query, which is bad practice but... practical for getting this monster of a query working.
            left outer join (select b3.VideoID, b3.VideoFile from Video_Question as a3 inner join Video as b3 on a3.QuestionID = b3.QuestionID) as vq 
    WHERE  
        st.StudentID = @StudentID
        and 
        s.SessionId = @SessionID
    ORDER  BY q.QuestionId 
    go
    
    警告: 您真的希望将其分解为单独的页面,以便在客户端应用程序中显示。例如,您需要为考试和ExamDetails显示主/详细信息,否则必须在显示给用户的输出中禁止重复值。 例如,您的输出在概念上如下所示:

    <Exam Detail Fields>    <Exam Detail Fields>
    first one unique        What is the color of your DOG?
    repeat          What is the color of your CAT?
    repeat          What is the color of your CAR?
    repeat          What is the color of your HAT?
    
    
    你的狗是什么颜色的?
    重复一遍你的猫是什么颜色的?
    重复一遍你的车是什么颜色的?
    重复一遍你的帽子是什么颜色的?
    

    请注意左侧的重复值,您必须抑制这些重复值。

    好吧,分小班进行尝试:我的意思是:-一部分一部分地执行查询 首先删除一些连接(删除大部分连接),然后逐个添加它们。我对庞大的查询执行相同的操作,以跟踪实际问题在哪里,或者哪些条件是获取结果行的主要区别。只是个主意。干杯

    我想

     LEFT JOIN Image_Question iq 
                  ON q.QuestionId = iq.QuestionNo 
    
    将为您提供空值

    当q.QuestionId从表格4开始,iq.QuestionNo为2时

    INNER JOIN Image i 
                   ON iq.ImageId = i.ImageId 
    

    而Iq字段中所有由左外部联接产生的值都是null。因此,它们上的任何内部联接都肯定会产生null结果

    这些表中的外键有问题:图像问题、音频问题和视频问题

    您使用的是QuestionNo列,该列未被Question表索引。 我建议将这些列替换为QuestionId,并使用它们的值

    Image_Question
    
    ImageQuestionId (PK) ImageId (FK) SessionId (FK)  QuestionNo(FK)
       1                    1             1                2
    
    Video_Question
    
    VideoQuestionId (PK) VideoId (FK) SessionId (FK)  QuestionNo(FK)
       1                      1            1               4
    
    Audio_Question
    
    AudioQuestionId (PK) AudioId (FK) SessionId (FK)  QuestionNo(FK)
       1                      1            1               5
    
    你的陈述应该是这样的:

    SELECT s.SessionId, 
           SessionName, 
           SessionDuration, 
           TotalMarks, 
           SessionWeight, 
           q.QuestionId, 
           q.QuestionNo, 
           QuestionContent, 
           QuestionMarks, 
           q.OptionId, 
           OptionType, 
           q.ReplyId, 
           ReplyType, 
           a.AnswerId, 
           Answer, 
           NoofAnswers, 
           AnswerMarks, 
           PenaltyEnabled, 
           PenaltyAnswerId, 
           PenaltyAnswer, 
           PenaltyMarks, 
           i.ImageId, 
           au.AudioId, 
           v.VideoId, 
           ImageFile, 
           AudioFile, 
           VideoFile, 
           s.ModuleId, 
           ModuleNo, 
           ModuleName, 
           ss.StudentId 
    FROM   Student st 
           INNER JOIN Student_Session ss 
                   ON st.StudentId = ss.StudentId 
           INNER JOIN Session s 
                   ON ss.SessionId = s.SessionId 
           INNER JOIN Question q 
                   ON s.SessionId = q.SessionId 
           INNER JOIN Answer a 
                   ON q.QuestionId = a.AnswerId 
           INNER JOIN Individual_Answer ia 
                   ON a.AnswerId = ia.AnswerId 
           LEFT JOIN Module m 
                  ON s.ModuleId = m.ModuleId 
           LEFT JOIN Penalty p 
                  ON q.SessionId = p.SessionId 
           LEFT JOIN Option_Table o 
                  ON q.OptionId = o.OptionId 
           LEFT JOIN Reply r 
                  ON q.ReplyId = r.ReplyId 
           LEFT JOIN Penalty_Marks pm 
                  ON q.QuestionId = pm.QuestionId 
           LEFT JOIN Image_Question iq 
                  ON q.QuestionNo= iq.QuestionNo
           LEFT JOIN Image i
                   ON iq.ImageId = i.ImageId 
           LEFT JOIN Audio_Question aq 
                  ON q.QuestionNo= aq.QuestionNo
           LEFT JOIN Audio au 
                   ON aq.AudioId = au.AudioId 
           LEFT JOIN Video_Question vq 
                  ON q.QuestionNo= vq.QuestionNo
           LEFT JOIN Video v 
                   ON vq.VideoId = v.VideoId 
    WHERE  s.SessionId = 1
    ORDER  BY q.QuestionId
    
    更新: 您可以使用QuestionNo列,毕竟,您可以添加索引,这样就不必修改代码了。呵呵

    我建议您添加以下内容:

    ALTER TABLE `your_schema`.`Question` 
    ADD INDEX `your_idx` (`QuestionNo` ASC);
    
    而不是这些:

      LEFT JOIN Image_Question iq 
        ON q.QuestionId = iq.QuestionId -- from iq.QuestionNo
      LEFT JOIN Image i
        ON iq.ImageId = i.ImageId 
      LEFT JOIN Audio_Question aq 
        ON q.QuestionId = aq.QuestionId -- from aq.QuestionNo
      LEFT JOIN Audio au 
        ON aq.AudioId = au.AudioId 
      LEFT JOIN Video_Question vq 
        ON q.QuestionId = vq.QuestionId -- from vq.QuestionNo
      LEFT JOIN Video v 
        ON vq.VideoId = v.VideoId
    
    您可以使用这些:

      LEFT JOIN Image_Question iq 
        ON q.QuestionNo= iq.QuestionNo
      LEFT JOIN Image i
        ON iq.ImageId = i.ImageId 
      LEFT JOIN Audio_Question aq 
        ON q.QuestionNo= aq.QuestionNo
      LEFT JOIN Audio au 
        ON aq.AudioId = au.AudioId 
      LEFT JOIN Video_Question vq 
        ON q.QuestionNo= vq.QuestionNo
      LEFT JOIN Video v 
        ON vq.VideoId = v.VideoId
    

    虽然我不是100%确定你想要的确切结果。根据您现有的查询,问题是您在某些表上使用
    内部联接
    ,而您应该使用
    左联接