0

I have a table like below

ID     Status
1      InActive
2      Active
3      Active
4      InActive
5      Active

I want to select ID 2 then get the status of ID 2 then based on that status I want to select all the remaining ID with the same Status.

I was not able to create a simple demo because I cant think of a way how to select.

Expected: Get ID 2,3,5 because they all have active status

Currently working on the problem at hand

Base select:

Select * from table where id = 2

I want to know what to add to this query to be able to select the other 2 row that share same status with the id 2

3
  • select id where status ="Active" Commented Sep 11, 2017 at 11:59
  • this will give only 1 result because ID is specified to be 2 Commented Sep 11, 2017 at 12:01
  • 1
    @MasivuyeCokile Close, but we don't necessarily know what the status for ID=2 is. To find that, we'd have to do some sort of query. Commented Sep 11, 2017 at 12:01

3 Answers 3

7

How about using a subquery to find the ID 2 status:

SELECT *
FROM yourTable
WHERE Status = (SELECT Status FROM yourTable WHERE ID = 2)
Sign up to request clarification or add additional context in comments.

Comments

3

You should use JOIN for your result.

SELECT *
FROM `tablename` AS t1
JOIN `tablename` AS t2 ON t1.Status = t2.Status where t1.ID = 2

Comments

1

Use the following query:

SELECT `ID` 
FROM `table` 
WHERE `Status` = (SELECT `Status` FROM `table` WHERE `ID` = 2)

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.