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
https://forums.phpfreaks.com/topic/3215-solved-sql-help/
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
https://forums.phpfreaks.com/topic/3215-solved-sql-help/#findComment-10956
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.