MySQL Query problem on join

Posted on 2009-04-02
Last Modified: 2013-12-12
I have a simple query running against MySQL through PHP.  I want to insert into a table all values that are in another table but not in that table.  This is the query I have:

INSERT Into DataNew (CollectorCode,CollectGross)
SELECT DISTINCT DataHistory.CollectorCode, 0
FROM DataHistory
LEFT OUTER JOIN DataNew ON DataNew.CollectorCode = DataHistory.CollectorCode
WHERE DataHistory.CodeType =1
AND DataHistory.CollectorCode NOT LIKE '%-b'
AND DataNew.CollectorCode IS NULL

The trouble is that the select query returns 602 rows whether I fileter by the DataNew.CollectorCode is Null or not.  How can I go about insertings into DataNew all Collectorcodes from DataHistory which are not already present?
Question by:GeoffSutton
  • 2
LVL 22

Expert Comment

ID: 24052807

I would do like this (if CollectorCode is unique)

INSERT Into DataNew (CollectorCode,CollectGross)
SELECT DISTINCT dh.CollectorCode, 0
FROM DataHistory dh
WHERE NOT EXISTS (SELECT 1 FROM DataNew dn WHERE dn.CollectorCode = dh.CollectorCode)

LVL 10

Author Comment

ID: 24053143
Unfortunately that does not work.  You query:
SELECT DISTINCT dh.CollectorCode, 0
FROM DataHistory dh
WHERE NOT EXISTS (SELECT 1 FROM DataNew dn WHERE dn.CollectorCode = dh.CollectorCode) AND
Returns the exact same results as
SELECT DISTINCT dh.CollectorCode, 0
FROM DataHistory dh
WHERE dh.CodeType=1

 They both show 602 rows, and datanew has 149 distinct codes entered.  I was reasonably sure that the SQL and the logic were good.  But being unfamiliar with MySQL I am wondering if the comparisons pay attention to potential trailing spaces and hidden characters?  I know that the collation is case insensitive.
Thanks for the quick response.  Hopefully we can resolve this right away.
LVL 22

Accepted Solution

pivar earned 500 total points
ID: 24053248
Just so I understand, CollectorCode is unique?

Can you show us the tabledefinition and some sampledata?

If you would like to trim the CollectorCode try

SELECT DISTINCT dh.CollectorCode, 0
FROM DataHistory dh
WHERE NOT EXISTS (SELECT 1 FROM DataNew dn WHERE TRIM(dn.CollectorCode)=TRIM(dh.CollectorCode)) AND

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP installation issues 11 59
Sql server function help 15 28
website maintenance mode 1 17
I've got an interview this morning and I want to sound intelligent... 4 47
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now