如何在查询中使用多个select语句

时间:2015-09-04 08:51:12

标签: select join union

我发现在查询中加入多个select语句很困难。 如果我选择一个查询它工作正常但加入select语句与union命令什么都不显示。我在做什么可能是错的。

$sel=mysql_query ("SELECT  *
FROM studentmark  join
     student  
     ON studentmark.student_id = student.username join
     subject 
     ON subject.code = studentmark.code 
 where student.username='$name' AND studentmark.YEAR = '$ya' AND
    studentmark.TERM = 'THIRD') 
//it works fine without using the union for a single query but joining the query there is nothing display

UNION(SELECT TOTAL AS secondterm 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
 AND studentmark.YEAR='$ya' 
 AND studentmark.TERM = 'SECOND')UNION(SELECT TOTAL AS firstterm 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
 AND studentmark.YEAR='$ya' 
 AND studentmark.TERM = 'FIRST'");

 $fetch=mysql_fetch_array($sel);
 $count=mysql_num_rows($sel);

1 个答案:

答案 0 :(得分:0)

$sel=mysql_query ("SELECT  YEAR, TERM, CODE, student_id, ContAss20, AsgClassWk10, Test2nd10,Exam60, Total,tname
FROM studentmark  join
     student  
     ON studentmark.student_id = student.username join
     subject 
     ON subject.code = studentmark.code 
 where student.username='$name' AND studentmark.YEAR = '$ya' AND
    studentmark.TERM = 'THIRD') 
//it works fine without using the union for a single query but joining the query there is nothing display

UNION(SELECT null, null, null, null, null, null, null,null, Total as secondterm, null 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
 AND studentmark.YEAR='$ya' 
 AND studentmark.TERM = 'SECOND')UNION(SELECT TOTAL AS firstterm 
FROM studentmark
   JOIN subject ON subject.code=studentmark.code
WHERE studentmark.student_id='$name' 
 AND studentmark.YEAR='$ya' 
 AND studentmark.TERM = 'FIRST'");

 $fetch=mysql_fetch_array($sel);
 $count=mysql_num_rows($sel);