【问题标题】:compare tables and fetch data in codeigniter在 codeigniter 中比较表并获取数据
【发布时间】:2019-01-10 06:26:51
【问题描述】:

我有两个表如下 1> 个用户 2>profile_action

id  name                uid profile_id  action_type
1   aa                  1   2           bookmark
2   bb                  2   3           view
3   cc                  1   3           like

现在我想要基于以下两个条件的数据

  • 其中 uid != profile_action 表中的 m_session_id
  • 并删除我喜欢或收藏的用户

所以我已经完成了以下操作

$bscDta = $this->db->select('profile_id')
                            ->from('profile_action')
                            ->where(array('user_id'=>1,'action_type'=>'like'))
                            ->or_where('action_type','bookmark')
                            ->get();
$exceptionalId = $bscDta->result();
foreach ($exceptionalId as $exid) {
    $excptnlId .= $exid->profile_id.",";
}
$excptnlId = rtrim($excptnlId,",");
$quickSrch = $this->db->select('*')
                    ->from('users')
                    ->where(array('id!='=>1,'status'=>'Active'))
                    ->where_not_in('id', $excptnlId)
                    ->get();
$quickSrchData = $quickSrch->result();

但它会返回以下查询

SELECT * FROM `users` WHERE `id` != 1 AND `status` = 'Active' AND `id` NOT IN('2,3')

和我/p我

[id] => 3 's data

其中预期的 o/p 为空白。那么有没有其他方法可以同时进行这两项操作 单次操作。

【问题讨论】:

  • 在提供的表结构和数据列名称为type,在查询中,您使用的是action_type
  • 第二个东西是where_not_in() 需要数组。所以,不要将数组转换成逗号分隔的字符串。
  • 我已经转换了,因为它会exceptionalId返回我Array ( [0] => stdClass Object ( [profile_id] => 2 ...
  • 然后转换成简单数组而不是字符串。
  • 我的问题是关于so is there any other way to do both operation in single operation

标签: php mysql sql codeigniter join


【解决方案1】:

JOIN 试试这样

$this->db->select('users.*, profile_action.profile_id');
$this->db->from('users');
$this->db->join('profile_action', 'users.id = profile_action.uid');
$this->db->where("(profile_action.action_type = like OR profile_action.action_type = bookmark)");
$this->db->where('user.user_id !=', 1);
$this->db->where('user.status', 'Acitve');
return $this->db->get()->result();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-05
    相关资源
    最近更新 更多