Solved

MySQL Query problem on join

Posted on 2009-04-02
3
221 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
  • 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

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

896 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

13 Experts available now in Live!

Get 1:1 Help Now