MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 11th, 2013, 11:17 AM
insight39 insight39 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 9 insight39 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 9 sec
Reputation Power: 0
Problem writing Subquery

I am trying to see if a product's model number or UPC code are listed inside a "do not show" table. I have written a query that looks to see if BOTH are found, but I need a query to looks for either one to be true.

ModelNumber in Table A = ModelNumber in Table B
or
UPC in Table A = UPC in Table B

Here is the query that isn't working. It looks for both to be true. How do I write this so that either are true.

Select distinct A.ModelNumber, A.upc
FROM A
WHERE (A.ModelNumber, A.UPC) NOT IN (select B.UPC, B.ModelNumber from B)
and A.ModelNumber= '#form.xyz#'

Thank you in advance!

Scott

Reply With Quote
  #2  
Old March 11th, 2013, 11:21 AM
insight39 insight39 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 9 insight39 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 9 sec
Reputation Power: 0
Follow up to above

When a modelnumber is passed to this query I realize I could write this differently, but this query also needs to find ALL NON-Matching products when a modelnumber is not passed to this query.

Reply With Quote
  #3  
Old March 11th, 2013, 06:12 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
Code:
SELECT a.modelnumber
     , a.upc
  FROM a
LEFT OUTER
  JOIN b
    ON b.modelnumber = a.modelnumber
    OR b.upc = a.upc
 WHERE a.modelnumber= '#form.xyz#'
   AND b.modelnumber IS NULL
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Reply With Quote
  #4  
Old March 11th, 2013, 07:49 PM
insight39 insight39 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 9 insight39 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 37 m 9 sec
Reputation Power: 0
Works with passed ModelNumber, but not without

Thank you for posting this query. The query works great when the model number is passed (#form.xyz#), but it hangs when trying to find ALL products from TABLE A that are not in TABLE B using the same two fields (ModelNumber and UPC).

For example, if I try to query all products from TABLE A that are not in TABLE B the query hangs. Even if I limit the query to the first 100 products (limit 0,100) it takes almost 5 seconds, making it unusable for a table with over 5,000 SKU's.

Any suggestions on a query to use when trying to find more than just a few matches?

Thanks again for your help.

Scott

Reply With Quote
  #5  
Old March 11th, 2013, 08:20 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 26,373 r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level)r937 User rank is General 47th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 1 Week 2 Days 6 h 54 m 49 sec
Reputation Power: 4140
try this --
Code:
SELECT a.modelnumber
     , a.upc
  FROM a
LEFT OUTER
  JOIN b
    ON b.modelnumber = a.modelnumber
 WHERE b.modelnumber IS NULL
UNION
SELECT a.modelnumber
     , a.upc
  FROM a
LEFT OUTER
  JOIN b
    ON b.upc = a.upc
 WHERE b.upc IS NULL

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Problem writing Subquery

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap