mysql中的Fname和姓氏,错误显示

时间:2015-05-16 15:05:44

标签: php mysql

我的考试系统有问题。在管理面板中,结果页面中显示正确的分数,但不显示名字和姓氏。我不知道该怎么做。

以下是代码:

$result1=executeQuery("select s.nume_student,s.prenume_student,s.stdname,s.stdid, IFNULL((select sum(q.marks) from studentquestion as sq,question as q where q.qnid=sq.qnid and sq.testid=".$_REQUEST['testid']." and sq.stdid=st.stdid and sq.stdanswer=q.correctanswer),0) as om from studenttest as st, student as s where s.stdid=st.stdid and st.testid=".$_REQUEST['testid'].";" );


$result3=executeQuery("SELECT sq.stdid, sq.testid, COUNT(*) AS correctAnswers, SUM(q.marks) AS studentScore,(SELECT SUM(marks) FROM question WHERE testid=".$_REQUEST['testid'].") AS totalScore FROM question q, studentquestion sq WHERE sq.testid=".$_REQUEST['testid']." AND q.testid = sq.testid AND q.qnid = sq.qnid AND sq.answered = 'answered' AND q.correctanswer = sq.stdanswer GROUP BY sq.stdid");

没有$ result1 doesen显示任何内容

以下是数据库中的表格:

问题表:

    CREATE TABLE IF NOT EXISTS `question` (
`testid` bigint(20) NOT NULL DEFAULT '0',
  `qnid` int(11) NOT NULL DEFAULT '0',
  `question` varchar(500) DEFAULT NULL,
  `optiona` varchar(100) DEFAULT NULL,
  `optionb` varchar(100) DEFAULT NULL,
  `optionc` varchar(100) DEFAULT NULL,
  `optiond` varchar(100) DEFAULT NULL,
  `correctanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL,
  `marks` float(3,2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

studentquestion表:

CREATE TABLE IF NOT EXISTS `studentquestion` (
  `stdid` bigint(20) NOT NULL DEFAULT '0',
  `testid` bigint(20) NOT NULL DEFAULT '0',
  `qnid` int(11) NOT NULL DEFAULT '0',
  `answered` enum('answered','unanswered','review') DEFAULT NULL,
  `stdanswer` enum('optiona','optionb','optionc','optiond') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

学生表:

CREATE TABLE IF NOT EXISTS `student` (
  `stdid` bigint(20) NOT NULL,
  `stdname` varchar(40) DEFAULT NULL,
  `stdpassword` varchar(40) DEFAULT NULL,
  `emailid` varchar(40) DEFAULT NULL,
  `contactno` varchar(20) DEFAULT NULL,
  `fname` varchar(40) DEFAULT NULL,
  `lname` varchar(40) DEFAULT NULL,
  `pincode` varchar(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

学生考试表:

CREATE TABLE IF NOT EXISTS `studenttest` (
  `stdid` bigint(20) NOT NULL DEFAULT '0',
  `testid` bigint(20) NOT NULL DEFAULT '0',
  `starttime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `endtime` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `correctlyanswered` int(11) DEFAULT NULL,
  `status` enum('over','inprogress') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

和测试表:

CREATE TABLE IF NOT EXISTS `test` (
  `testid` bigint(20) NOT NULL,
  `testname` varchar(30) NOT NULL,
  `testdesc` varchar(100) DEFAULT NULL,
  `testdate` date DEFAULT NULL,
  `testtime` time DEFAULT NULL,
  `subid` int(11) DEFAULT NULL,
  `testfrom` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `testto` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `duration` int(11) DEFAULT NULL,
  `totalquestions` int(11) DEFAULT NULL,
  `attemptedstudents` bigint(20) DEFAULT NULL,
  `testcode` varchar(40) NOT NULL,
  `tcid` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我解决了同样的问题:

How to get sql WHERE statement auto update from localhost link

但是我用fname和姓氏来到这里。

2 个答案:

答案 0 :(得分:0)

如果您的第二个查询返回正确的学生ID,您只需更改它即可添加学生姓名:

SELECT s.lastname, s.firstname, e.* 
from 
students s join (
  ... your second query ... ) e
on s.stdid=e.stdid

您可以根据自己的需要更改联接类型。

答案 1 :(得分:0)

谢谢,但我解决了这个问题:

SELECT ***s.fname, s.lname***, sq.stdid, sq.testid, COUNT(*) AS correctAnswers, 
   SUM(q.marks) AS studentScore,(SELECT SUM(marks) FROM question 
   WHERE testid=1) AS totalScore  
   FROM question q, studentquestion sq, ***student s*** 
   WHERE sq.testid=1 AND q.testid = sq.testid 
   AND q.qnid = sq.qnid AND sq.answered = 'answered' ***and sq.stdid=s.stdid***
   AND q.correctanswer = sq.stdanswer GROUP BY sq.stdid