Jump to content

Finding Records Where a JOIN doesnt exist


jh_dempsey

Recommended Posts

Hey All

 

At the moment ive got two tables. One holds all the entrants details like their competitor number (bib number), name, and the events they have entered (k1 and c1) The other holds the competitors score for the event.

 

nsr_entrants:

 

| id | bibnumber | name | k1| c1 |

 

 

k1_slalom_scores:

 

| entrant_id | score |

 

 

I want to be able to find out if there are any competitors who have been assigned a bib number (ie their bib number is NOT zero), are taking part in the K1 event, and have not been given a score (ie. their entrant_id doesnt exist in the k1_slalom_scores table). If i find any, then i want to output their names to a table.

 

Looking through the mySQL manual i found this which will do roughly what i want:

 

If there is no matching record for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table. You can use this fact to find records in a table that have no counterpart in another table: 

mysql> SELECT table1.* FROM table1
    ->        LEFT JOIN table2 ON table1.id=table2.id
    ->        WHERE table2.id IS NULL;

This example finds all rows in table1 with an id value that is not present in table2 (that is, all rows in table1 with no corresponding row in table2). This assumes that table2.id is declared NOT NULL. See Section 7.2.8, “How MySQL Optimizes LEFT JOIN and RIGHT JOIN”.

 

Just to keep things simple to start with, i just tried to get the simple join going. I would try and get the other parameters (like being entered in K1 and bib number not equal to zero) added on later.

 

I tried modifying the above to my table like below but had no luck

 

SELECT *.nsr_entrants FROM nsr_entrants LEFT JOIN k1_slalom_scores ON nsr_entrants.id = k1_slalom_scores.entrant_id WHERE k1_slalom_scores.entrant_id IS NULL

 

Can anyony help me out and tell me what im doing wrong??

 

 

 

 

Link to comment
Share on other sites

[!--fonto:Courier New--][span style=\"font-family:Courier New\"][!--/fonto--]*.nsr_entrants[!--fontc--][/span][!--/fontc--]? did you mean [!--fonto:Courier New--][span style=\"font-family:Courier New\"][!--/fonto--]nsr_entrants.*[!--fontc--][/span][!--/fontc--]?

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.