[英]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?如何通过获取$_SESSION['admin']['id']
获取所有值,找到该管理员的 country_id 并获取与会话管理员相同的所有其他 admin_id 和 country_id?
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.所以,假设当前登录的 admin 有id
= 5 ,这意味着 admin_id: 5 有两个 country_id: 1 和 62。我想获取所有具有 country_id: 1 和 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?如何在一个 sql 查询中执行此操作?
You can use a where
clause for filtering on the admin_id
or the country_id
:您可以使用where
子句过滤admin_id
或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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.