【问题标题】:How can i join table with second table latest record我如何将表与第二个表的最新记录连接起来
【发布时间】:2019-07-19 07:58:08
【问题描述】:

我有两个表,如注册表和 Academicdetails 表,根据注册 ID,我在学术表中有两条记录,但我想加入最新的学术表记录。

查询:

SELECT a.*, b.status AS ac_status 
FROM registration AS a 
LEFT JOIN academicdetails AS b ON a.id = b.registration_id 
WHERE b.status = '1' 
ORDER BY id DESC 
LIMIT 100

PHP 代码:

if(isset($search['category']) && $search['category']!=''){
         $condition = ', TIMESTAMPDIFF(YEAR, a.dob, CURDATE()) AS age';
    }

    $this->db->select('a.*,b.status as ac_status'.$condition);
    $this->db->from("registration as a");
    $this->db->join('academicdetails as b','a.id = b.registration_id','left');
    if(isset($search['search_by_name']) && $search['search_by_name']!=''){
         $this->db->where('a.name', $search['search_by_name']);
    }
    if(isset($search['gender']) && $search['gender']!=''){
         $this->db->where('a.gender', $search['gender']);
    }

    if(isset($search['status']) && $search['status']!=''){
            $this->db->where('b.status', $search['status']);
    }

    if(isset($search['category']) && $search['category']!=''){
         if($search['category']=='Youth'){
          $this->db->where('age>10 AND age<18');
         }elseif($search['category']=='Junior'){
          $this->db->where('age>17 AND age<22');
         }elseif($search['category']=='Senior'){
          $this->db->where('age > 21');
         }
    }

    if(isset($search['course']) && $search['course']!=''){
         $this->db->where('b.course', $search['course']);
    }

    $this->db->limit($search['limit'], $search['start']);
    $this->db->order_by($this->id, $this->order);
    $this->db->group_by('a.id');
    $query = $this->db->get();

【问题讨论】:

  • 看起来这是PHP代码,但是你使用的是什么框架?代码是否相关,或者您可以发布 MySQL 查询吗?抱歉,您的问题很难理解。
  • 代码点火器中的这段代码
  • SELECT a.*, b.status as ac_status FROM registration as a LEFT JOIN academicdetails as b b3449 @987654345。 @ = b.registration_id WHERE b.status = '1' ORDER BY id DESC LIMIT 100
  • 我将您的查询添加到问题中,以帮助读者。

标签: php mysql codeigniter codeigniter-3


【解决方案1】:

请尝试以下查询:

SELECT * FROM `registration` LEFT JOIN academicdetails on registration.id = academicdetails.id ORDER BY academicdetails.id desc LIMIT 100;

【讨论】:

    【解决方案2】:

    试试这个:-

    $this->db->select("a.*, b.status AS ac_status");
    $this->db->from('registration AS a');
    $this->db->join('academicdetails AS b', 'b.registration_id = a.id', 'left');
    $this->db->where("b.status",1);
    $this->db->order_by("b.id","DESC");//primary key or id of your academic table.
    $this->db->limit(100);
    $this->db->get();
    

    【讨论】:

      猜你喜欢
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多