Skip to content
Advertisement

How to fetch all values of a column based on value of another column in same table?

I got the following structure:

admin_id  ||  country_id 
---------------------------------
 1              2      
 5              1
 1              2
 2              3
 5              62
 1              1
 3              62

How to fetch all values by taking the $_SESSION['admin']['id'], finding the country_id of that admin and getting all other admin_id and country_id that are the same of the session admin?

So, lets say the currently logged in admin has id = 5 , that means the admin_id: 5 has two country_id: 1 and 62. I want to take all rows that have country_id: 1 and 62.

It should return this:

admin_id || country_id 
------------------------        
 5              1  
 5              62
 1              1
 3              62

How can I do this in one sql query?

Advertisement

Answer

You can use a where clause for filtering on the admin_id or the country_id:

select t.*
from t
where t.admin_id = 5 or
      t.country_id in (select t2.country_id from t t2 where t2.admin_id = 5);
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement