Jump to content

[SOLVED] SQL Help


tobeyt23

Recommended Posts

I am trying to pull only the zips that there ids are not in the zip_tracker table, but when i do so it only sees the first enter that is in the zip_tracker table and show the rest along with everything else that should be shown. The purpose is someone looks up a zip selects it and i place it in the zip_tracker table so when someone else looks up the zip again they can not select it. What is wrong with this:

[code]
SELECT zip_code.* FROM zip_code RIGHT JOIN zip_tracker on zip_code.zip_code_id!=zip_tracker.zip_code_id and zip_tracker.mailer_date='0000-00-00' WHERE zip_code LIKE '0%'
[/code]
Link to comment
Share on other sites

First, you want a LEFT JOIN, not a RIGHT JOIN. Second, if you want records that do _not_ show up in the "right" table, you need to add a "WHERE rightTable.id IS NULL". Third, you'd need to change your ON clause to have an equality relating the primary key to the foreign key. Fourth, I'm not sure what you mean by the first number -- that's in your WHERE clause already.

The following query (UNTESTED) will find the "non-tracked" zip codes:

[code]SELECT zip_code.* FROM zip_code LEFT JOIN zip_tracker ON zip_code.zip_code_id=zip_tracker.zip_code_id WHERE zip_tracker.zip_code_id IS NULL[/code]

Feel free to add whatever other clauses are necessary.
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.