繁体   English   中英

sql结果来自另一个包含子字段的表的表匹配字段

[英]sql results from a table matching field from another table including sub fields

使用codeigniter查询,我无法获得适当的结果,我需要以这种方式获取表的结果

$catid=$_POST['category'];
$new_id = select catid from categories_table where catid='$catid' and parent='$catid';

因此它还会添加其他以$ cadid作为父项的猫的结果

Select * from articles_table where catid = '$new_id';

我正在像这样在codeigniter中尝试

  $p=$this->input->post('category');
  $this->db->select('catid');
  $this->db->where('parent',$p);
  $this->db->where('catid',$p);
  $query = $this->db->get('categories_table');

  if($query->num_rows()>0)
             {
             foreach($query->result() as $row)
             $new_id[]=$row->catid;
             }
  $this->db->where('catid',$new_id);
  $this->db->where('status',1);
  $this->db->order_by('time_added', 'desc');  
  $res = $this->db->get('articles_table');
  $query_res= $res->result();

它给出错误Message: Trying to get property of non-object

cats table
catid -- parent -- name
1      -- 0          -- first cat
2  --     1         -- cat child of first

Article table
id -- catid - content
1  -- 2     -- first article
2 --  1     -- second article

如果我查询cat id = 1的地方,它也应该从catid 2返回结果,因为2是1的孩子

我会尝试使用类似这样的模型:

$p = $this->input->post('category');
$query = $this->db
    ->select('catid')
    ->where('parent',$p)
    ->or_where('catid',$p)
    ->get('categories_table');

$data = array();
if($query->num_rows())
{
   foreach($query->result() as $row)
   {
       $res = $this->db
            ->where('catid',$row->catid)
            ->where('status',1)
            ->order_by('time_added', 'desc') 
            ->get('articles_table');
       foreach ($res->result() as $article_data)
       {
          $data[] = $article_data;
       }
    }
}
return $data;

代码首先检查category_table中$p等于catidparent的类别。 然后检查具有相同的物品catid作为类别,并添加到阵列中的所有物品, $data

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM