개발 Q&A

제목 join. 가장 최신 가장 최근 등록한 값을 보여주고 싶은데, 혹시 아시는 분 계실까요?
카테고리 PHP
글쓴이 그동안 작성시각 2021/02/21 17:44:59
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 5872   RSS

안녕하십니까, 궁금한게 있어 문의 드립니다.

아래 쿼리에서 join AS d를 걸어서, d.recruit_state 값을 조회 했습니다. 여기까지는 이슈가 없습니다.

여기서 d.recruit_state 값을 가장 최근 등록한 값을 보여주고 싶은데, 어떻게 쿼리를 수정해야 할지 모르겠네요.

혹시 아시는 분 계실까요?

 

 

    function admin_estimate_list($request_basic_idx)
    {                
        $this->db->select('
            a.*,
            group_concat(DISTINCT b.job_type) AS job_type ,
            c.request_basic_idx, c.request_name, c.subscribe_insert_kakao_send_yn,
            d.recruit_state,
        ');

        $this->db->from('estimate_basic AS a');
        $this->db->join('estimate_job_type AS b','a.estimate_basic_idx = b.estimate_basic_idx');
        $this->db->join('request_basic AS c','a.request_basic_idx = c.request_basic_idx'); 
        $this->db->join('estimate_recruit_state AS d','a.estimate_basic_idx = d.estimate_basic_idx' , 'left');         
        $this->db->where('a.request_basic_idx', $request_basic_idx);       
        $this->db->order_by('a.created_at','desc');        
        $this->db->group_by("a.estimate_basic_idx"); 
        $query = $this->db->get();
        $result = $query->result();
        return $result;
    }

 

 다음글 플러그인 스크립트 없이 정렬이 가능할까요? (2)
 이전글 변수를 함수로 사용가능한가요? (1)

댓글

변종원(웅파) / 2021/02/22 07:14:21 / 추천 0
order by 추가해보셨는지요?
그동안 / 2021/02/22 09:54:05 / 추천 0

웅파님, order_by를 아래와 같이 넣어봤는데, 아니더군요.

    function admin_estimate_list($request_basic_idx)
    {                
        $this->db->select('
            a.*,
            group_concat(DISTINCT b.job_type) AS job_type ,
            c.request_basic_idx, c.request_name, c.subscribe_insert_kakao_send_yn,
            d.recruit_state,d.created_at
        ');

        $this->db->from('estimate_basic AS a');
        $this->db->join('estimate_job_type AS b','a.estimate_basic_idx = b.estimate_basic_idx');
        $this->db->join('request_basic AS c','a.request_basic_idx = c.request_basic_idx'); 
        $this->db->join('estimate_recruit_state AS d','a.estimate_basic_idx = d.estimate_basic_idx' , 'left');           
        $this->db->where('a.request_basic_idx', $request_basic_idx);                                                     
        //$this->db->order_by('d.created_at','desc');   <===이부분이 넣어본 부분입니다.
        $this->db->order_by('a.created_at','desc');        
        $this->db->group_by("a.estimate_basic_idx");                                                              
        $query = $this->db->get();
        $result = $query->result();                                                                               
        return $result;
    }

 

PureAni / 2021/02/22 11:39:02 / 추천 0

d 테이블 row 를 마지막꺼를 가져와서 join 하도록 하세요.

max(`created_at`) 잘 활용하시면 될겁니다.

보통 join 하면 db 의 기본 정렬로 가져오다보니, 마지막 row 가 필요하면, join 시점부터 마지막 row 를 가져오도록 해야합니다.