0

Hi were trying to perform a mysql select which isnt going to plan and hoping someone can shed some light on it.

we have estimated 10,000 plus listing records, a customer can have several listing records for different locations. we need to select all customer listings where at least one of the locations is equal to a specifield location.

so for example lets say customer 1 has a listing in sheffiled, doncaster, leeds, wakefield and customer 2 has listings in london and brighton.

Now I want to select all customer listings where one of the listings is for the area sheffield.

Id hope to get back the 4 rows for customer 1 because one of his listings is in sheffield.

for the sake of this example lets just presume the table consists of just customerId and LocationName

I need to select all customerIds where one of the locationNames = sheffield. So Id get 4 rows retruend with the cusotmer ID and the 4 locations

How do you write this query in mysql? Im guessing subselect but not too sure.

2 Answers 2

4
SELECT customerid FROM customers_location
WHERE customerid IN(SELECT DISTINCT customerid FROM customers_location WHERE LocationName = 'sheffield')
Sign up to request clarification or add additional context in comments.

Comments

2

Something like:

SELECT * FROM CUST_TABLE WHERE CUST_ID IN (
   SELECT DISTINCT CUST_ID FROM CUST_TABLE WHERE CUST_LOCN='Sheffield')

Note; The distinct clause may not be strictly necessary, not sure. That would give you eg. 4 records for customer xyz who has one of their listing locations as Sheffield, which I think is what you're asking.

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.