0

I have a table with those fields: phID, ProductID

The phID is the pharmacy id and the ProductID is the product id.

  • One pharmacy has multiple products
  • 1 product is to multiple pharmacies

Example:

phID    ProductID
-----------------
1001    9
1001    10
1001    11
1004    9
1004    12
1004    14
1004    11

The query that I want is so I can get all the phID that has the same product. I have this query:

SELECT phID, ProductID
FROM ph_pd
WHERE ProductID IN (9,10,11) 

I want the results to be

1001 9
1004 9
1001 11
1004 11

or just

1001
1004
1

3 Answers 3

1

If you want to get distinct phID which are fall under the certain productID, you have to use DISTINCT...

  SELECT distinct phID FROM ph_pd WHERE ProductID IN (9,10,11) 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use :

SELECT p.phID , p.ProductID FROM ph_pd p
WHERE (select count(*) from ph_pd where ProductID = p.ProductID) >= 2
ORDER BY ProductID ;

Comments

0
SELECT  phID
FROM    tableName
WHERE   productID IN (9,10,11)
GROUP   BY phID
HAVING  COUNT(DISTINCT productID) = 2 -- since there are two stores

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.