如何在laravel中编写这种类型的查询?

时间:2014-02-06 06:16:56

标签: php codeigniter laravel laravel-4

这是Codeigniter Active记录查询如何在Laravel-4查询中写这个?

    public function bring_project_sub_sector5($id = 0) {
    if ($id != 0) {
        $this->db->select("t2.sector_name_en AS sub_sector_name");
        $this->db->from("biz_r_proj_activity AS t1, biz_s_sub_sector AS t2");
        //$this->db->join("biz_s_sub_sector AS t2","t2.id = t1.sub_sector_id","left");
        $this->db->where("t1.sub_sector_id = t2.id");
        $this->db->where("t1.id", $id);
        $this->db->group_by('t2.id');
        $query = $this->db->get();
        if ($query) {
            return $query;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
}

1 个答案:

答案 0 :(得分:2)

这样的事情,假设你没有该表的模型

$query = DB::table('biz_r_proj_activity AS t1')
            ->join('biz_s_sub_sector AS t2', 't1.sub_sector_id', '=', 't2.id')
            ->where('t1.id', $id)
            ->groupBy('t2.id')
            ->get(array('t2.sector_name_en AS sub_sector_name'));