0

I have a query which is fetching values from multiple tables, it looks some thing close to the below query.

I have highlighted in bold the place where i need to pass the value returned from the same query.

 select E.[EmployeeName] as EmployeeName
, (select City from tblEmployeeCities where C.EmployeeID = E.EmployeeID) as EmployeeCity
, (Select State from tblStates **where City =  /i need to give the name of 
the city returned by the above statement**  ) as EmployeeState
from tblEmployees E
1

3 Answers 3

1

Use JOIN/LEFT JOIN:

SELECT E.EmployeeName, C.City, S.State
FROM tblEmployees E
LEFT JOIN tblEmployeeCities C ON C.EmployeeID=E.EmployeeID
LEFT JOIN tblStates S ON S.City=C.City
Sign up to request clarification or add additional context in comments.

1 Comment

Except you need to replace the WHERE in the first JOIN with ON.
0

Maybe something like

select E.[EmployeeName] as EmployeeName
    , c.City as EmployeeCity
    , (Select State from tblStates where City = c.City ) as EmployeeState
from tblEmployees E
    inner join tblEmployeeCities C on C.EmployeeID = E.EmployeeID

Comments

0

I think this can be achieved with a CASE WHEN ELSE END statment in the select part of your sql statment.

Look at example here for syntax or usage: https://www.w3schools.com/sql/func_mysql_case.asp

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.