!= operator in queries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aaron C

    #1

    != operator in queries

    I have a query with the following syntax to find students that haven't
    voted:

    SELECT * FROM Students
    WHERE Students.Studen tID != Voted.StudentID

    The thing is, the results I'm getting are basically the same as if has
    just used 'SELECT * FROM Students' alone. The StudentID list of 'Voted'
    is shorter than that of 'Students', as some students haven't voted yet.
    Anyone know what I'm wording wrong here?

    Thanks,
    Aaron
  • Aggro

    #2
    Re: != operator in queries

    Aaron C wrote:[color=blue]
    > I have a query with the following syntax to find students that haven't
    > voted:
    >
    > SELECT * FROM Students
    > WHERE Students.Studen tID != Voted.StudentID
    >
    > The thing is, the results I'm getting are basically the same as if has
    > just used 'SELECT * FROM Students' alone. The StudentID list of 'Voted'
    > is shorter than that of 'Students', as some students haven't voted yet.
    > Anyone know what I'm wording wrong here?[/color]

    Yep, you are selecting rows only from Students table, but using column
    from Voted table. It is like comparing the id value to NULL. (Amazing
    that it doesn't give syntax error..)

    Try something like:
    ( Not tested, might contain syntax errors, it's 1:30 am here... )

    SELECT Students.* FROM Students
    LEFT JOIN Voted on Students.Studen tID = Voted.StudentID
    WHERE Voted.StudentID IS NULL;

    This will find all connections between these two tables. And if there is
    no connection (Voted.StudentI D is null), the row is added to result list.

    Comment

    Working...