我有下表
id group_id user_id
1 2 11
2 2 12
3 2 13
4 3 11
5 3 12
6 4 11
现在我想要公共(public)组 ID 表示如果
I pass user_id (11,12) then result will be group_id (2 and 3)
I pass user_id (11) then result will be group_id (2,3 and 4)
I pass user_id (11,12,13) then result will be group_id (2)
I pass user_id (13,15) then result will be zero group_id (NULL)
我正在使用 CodeIgniter,但您可以在核心 SQL 方面向我提出建议。如果您的答案符合 CI 模型(查询)标准,那就太好了。
最佳答案
您可以使用distinct
和where_in
来获得您想要的结果。其中 array
包含您的 user_id
$this->db->distinct();
$this->db->select('group_id');
$this->db->from('your_table');
$this->db->where_in('user_id',array('11','12');//you can pass here 11,12,13 or 11 or 11,12
对于和条件
$array = array('user_id' => 11, 'user_id' => 12);
$this->db->distinct();
$this->db->select('group_id');
$this->db->from('your_table');
$this->db->where($array);
关于php - Codeigniter:获取相同值之间的公共(public)列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32395851/