Jump to content

SELECT


ec

Recommended Posts

I dunno what's wrong with this

 

$query = "SELECT teacherid, position FROM teacher WHERE teacherid NOT IN "members" ";

 

 

I'm trying to get the query  to select teacherid and position from a table called teacher provided that the teacherid doesn't appear in the table members as well...

 

any ideas??

Link to comment
Share on other sites

I can only forsee 1 problem with the SQL provided. If the members table becomes very large the IN (SELECT...) part of the statement will become very slow, because you're selecting ALL the members from the table regardless of anything else.

 

Try:

SELECT t.teacherid as 'teacherid'
      ,t.position as 'position'
FROM teachers t 
LEFT JOIN members m ON t.teacherid = m.teacherid
WHERE m.teacherid IS NULL

 

This does a LEFT JOIN based on the id of the teacher (which i'm hoping is indexed at least), so theoretically you will ONLY ever pull 1 row from the members table.

Link to comment
Share on other sites

try

<?php
$q = "select teacher.teacherid as teacherid, teacher.position as position from `teacher` where teacher.teacherid NOT IN (select member.teacherid from `members`)";
?>

so long as teacherid is the foreign key in members

Ugh... a subquery with NOT IN... definitely use aschk's code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.