代码之家  ›  专栏  ›  技术社区  ›  Che Jug

将3张桌子和PHP连接起来

  •  0
  • Che Jug  · 技术社区  · 5 年前

    最初,我有两个表Item和ItemFavorite。当我想要收藏的物品时,我会这样选择:

     @Query("SELECT prd.* FROM Item prd, ItemFavourite fp WHERE prd.id = fp.itemId order by fp.sorting ")
    

        function get_item_favourite( $conds = array(), $limit = false, $offset = false  )
    {
        $this->db->select('bs_items.*'); 
        $this->db->from('bs_items');
        $this->db->join('bs_favourite', 'bs_favourite.item_id = bs_items.id');
        
    
        if(isset($conds['user_id'])) {
    
            if ($conds['user_id'] != "" || $conds['user_id'] != 0) {
                    
                    $this->db->where( 'user_id', $conds['user_id'] );   
    
            }
    
        }
    
        if ( $limit ) {
        // if there is limit, set the limit
            
            $this->db->limit($limit);
        }
        
        if ( $offset ) {
        // if there is offset, set the offset,
            $this->db->offset($offset);
        }
        
        return $this->db->get();
        // print_r($this->db->last_query());die;
        
    }
    

    我这样试过:

     @Query("select * from ItemFavourite fp left join Item prd on fp.itemId = prd.id left join ItemMoto im on fp.itemId = im.id")
    LiveData<List<Item>> getAllFavouriteProducts();
    

    和php:

        function get_item_favourite( $conds = array(), $limit = false, $offset = false  )
    {
        $this->db->select('*'); 
        $this->db->from('bs_favourite');
        $this->db->join('bs_items', 'bs_items.id = bs_favourite.item_id');
        $this->db->join('bs_items_moto', 'bs_items_moto.id = bs_favourite.item_id', left);
    
        if(isset($conds['user_id'])) {
    
            if ($conds['user_id'] != "" || $conds['user_id'] != 0) {
                    
                    $this->db->where( 'user_id', $conds['user_id'] );   
    
            }
    
        }
    
        if ( $limit ) {
        // if there is limit, set the limit
            
            $this->db->limit($limit);
        }
        
        if ( $offset ) {
        // if there is offset, set the offset,
            $this->db->offset($offset);
        }
        
        return $this->db->get();
        // print_r($this->db->last_query());die;
        
    }
    

    我有两个最喜欢的项目和一个项目。当请求最喜爱的活动时,仅显示两个,即重复项MOTO。

    0 回复  |  直到 5 年前