Solved

MySQL Query problem on join

Posted on 2009-04-02
3
226 Views
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?
0
Comment
Question by:GeoffSutton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24052807
Hi,

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)

/peter
0
 
LVL 10

Author Comment

by:GeoffSutton
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
dh.CodeType=1
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.
Geoff
 
0
 
LVL 22

Accepted Solution

by:
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
dh.CodeType=1
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
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.

710 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