Where clause in Codeigniter Query Builder Class

时间:2016-10-20 19:42:14

标签: codeigniter query-builder

I got in trouble trying to get data from two MySQl table. In the first table I stored image filename; in the second table I stored info about each image; info are in two languages, en and it. Of course not all images in first table have info in the second one. I'm trying to get images an info for a selected language in the following way:

$this->db->select ('
            tbl_images.image,
            tbl_infos.info
            ');

$this->db->from('tbl_images');
$this->db->join('tbl_infos', 'tbl_images.id = tbl_infos.id', 'left');
$this->db->where('tbl_images.id', $id);
$this->db->where('tbl_infos.lang', 'en');

Now...if the tbl_infos table has text, the query return correct data, but if no info are stored I get an error since no images are retrieved; if I remove the lang where clause I get the iamge if no info are stored...but I get images with info twice!!

I'm getting crazy and completely stuck on this...how can I fix this? Thanks a lot

2 个答案:

答案 0 :(得分:0)

试试这个

$ this-> db-> join('tbl_infos','tbl_images.id = tbl_infos.id');

答案 1 :(得分:0)

愚蠢的我...我修复了在同一个JOIN行写第二个条件。 那么,接下来的JOIN:

$this->db->join('tbl_infos', 'tbl_images.id = tbl_infos.id', 'left');

变为:

$this->db->join('tbl_infos', 'tbl_images.id = tbl_infos.id AND tbl_infos.lang = "en"', 'left');