简体   繁体   中英

Can't build a query using CodeIgniter query builder

I need to build this query :

SELECT p.*, c.nazwa, c.url 
FROM posty p 
INNER JOIN posty_kategorie pc
ON pc.id_posta = p.id 
INNER JOIN kategorie c 
ON c.id = pc.id_kategorii
WHERE p.url = "przykladowy-tytul-strony"

Using CodeIgniter's Query Builder class.

All I have for now is:

return $this->db->from('posty')
                ->join('posty_kategorie', 'posty_kategorie.id_posta = posty.id', 'inner')
                ->join('kategorie', 'kategorie.id = posty_kategorie.id_kategorii')
                ->where('posty.url', $url)
                ->get()->row();

But it this doesn't work like I want it to. It has to make this query:

SELECT p.*, c.nazwa, c.url 
FROM posty p 
INNER JOIN posty_kategorie pc
ON pc.id_posta = p.id 
INNER JOIN kategorie c 
ON c.id = pc.id_kategorii
WHERE p.url = "przykladowy-tytul-strony"

But I do not know how to put this all together : < .

You can try this :

$this->db->select('p.*,c.nazwa,c.url');
$this->db->from('posty p');
$this->db->join('posty_kategorie pc ', 'pc.id_posta = p.id','inner');
$this->db->join('kategorie c ', 'c.id_posta = pc.id_kategorii','inner');
$this->db->where('p.url','przykladowy-tytul-strony');
$query = $this->db->get();
return $query->result();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM