php - Codeigniter:获取相同值之间的公共(public)列值

标签 php mysql codeigniter

我有下表

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 模型(查询)标准,那就太好了。

最佳答案

您可以使用distinctwhere_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/

相关文章:

php - codeigniter 日历可以用两种不同的颜色设置样式吗?

php - 如何在 Windows 上安装 Zend OPcache Extension + PHP >= 5.4

php - mysql | mysql |来自空列和 FIND_IN_SET 的请求

PHP MySQL - 未考虑字符集

mysql - SQL 查询中的时间周期问题。

mysql - Zabbix 数据库升级失败。无法添加或更新子行: a foreign key constraint fails

php - Access-Control-Allow-Origin 未出现在 codeigniter 的响应 header 中

javascript - 插入数据后jquery更新下拉列表的div

php - 如何用php检测当前页面?

php - 基于 PHP codeigniter 的网站上的搜索选项