SQL- How do I use the "IN" Attribute correctly?
-
I have a Query I need to make that shows all the products a company hasn't ordered. I figure I have to use an "IN" attribute, but I haven't wrapped my head around it yet. Here's what I have so far to make the products show. Any help is appreciated. and if more info is needed, let me know, I figured this bit of SQL I've come up with might be enough. SELECT Products.ProductID, Products.ProductName, Customers.CompanyName FROM Customers, Products, Orders, [Order Details] WHERE Customers.CustomerID = Orders.CustomerID AND Orders.OrderID=[Order Details].OrderID AND Products.ProductID =[Order Details].ProductID AND Customers.CompanyName = 'Bingo store' (SELECT ???? FROM ???? WHERE ????)
-
Answer:
Well if you are trying to do a sub select, this is how you would do it: SELECT Products.ProductID, Products.ProductName, Customers.CompanyName FROM Customers, Products, Orders, [Order Details] WHERE Customers.CustomerID = Orders.CustomerID AND Orders.OrderID=[Order Details].OrderID AND Products.ProductID =[Order Details].ProductID AND Customers.CompanyName = 'Bingo store' And ProductID in (SELECT ProductID FROM Products WHERE ProductName='Toys')
swilliam... at Yahoo! Answers Visit the source
Other answers
I would personally not use EXISTS, instead I would use NOT IN... SELECT Products.ProductID, Products.ProductName, Customers.CompanyName FROM Customers, Products, Orders, [Order Details] WHERE Customers.CustomerID = Orders.CustomerID AND Orders.OrderID=[Order Details].OrderID AND Products.ProductID =[Order Details].ProductID AND Customers.CompanyName = 'Bingo store' AND Products.ProductID NOT IN (SELECT DISTINCT t1.ProductID FROM Orders t1 WHERE t1.CompanyName = Customers.CompanyName)
The correct Where clause would be Where myColumnName in ('string1', 'string2', 'string3') So if myColumnName equals string1 or string2 or string3, that satisfies the condition. If you're looking to make the strings dynamic (like a variable), there's not a simple way to do that. In your case, I don't think IN is appropriate, and you should look into the EXISTS statement.
Related Q & A:
- How should I use Youtube API?Best solution by Stack Overflow
- How do I use "John et. al. 1990 " citation for more than two authors?Best solution by tex.stackexchange.com
- How do I create a digital signature and how do I use it?Best solution by support.office.com
- Where do I find my Yahoo briefcase and how do I use it?Best solution by Yahoo! Answers
- How do I use chat when I am in a room?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.