1

does anyone knows what's going on with this query please ?. What I am trying to do is if courseID in elective_modules table doesn't exist in group_elective_modules table, then print all exit in group_elective_modules. But I keep getting the error Operand should contain 1 column(s)

Here is my query

$alternativeEQuery = 
"SELECT elective_modules.courseID,elective_modules.yr 
 FROM elective_modules
 WHERE elective_modules.yr = '$year1' 
 AND elective_modules.courseID 
 NOT IN 
     (SELECT group_elective_modules.moduleID,group_elective_modules.courseName 
 FROM group_elective_modules 
 WHERE group_elective_modules.courseName = '$courseTitle'
 AND elective_modules.courseID = group_elective_modules.moduleID)";

$alternativeEResult = mysql_query($alternativeEQuery) or die($alternativeEQuery."<br/><br/>".mysql_error());
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
user1444442
  • 109
  • 4
  • 11
  • You have the possibility of an SQL injection attack. – Joe Aug 08 '12 at 09:39
  • @Joe.I already escaped all the variables above of code before I pass them as a parameter !! Thanks – user1444442 Aug 08 '12 at 09:45
  • The `IN()` operator expects 1 column if you are using a subquery to supply values. You can't select more than 1 column. If you think about it, it's logical also. – N.B. Aug 08 '12 at 09:46
  • @N.B.Thanks,that does make sense when you mentioned **IN** operator. Thank you!! – user1444442 Aug 08 '12 at 09:56

1 Answers1

4

You can't have two columns in the subquery here:

(SELECT group_elective_modules.moduleID,group_elective_modules.courseName 
 FROM group_elective_modules 
 WHERE group_elective_modules.courseName = '$courseTitle'
 AND elective_modules.courseID = group_elective_modules.moduleID)

Either use the right one, or add in a second not in clause and use the other column name there.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80