CodeIgniter Forums
I need to get data from different table using sql query - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: I need to get data from different table using sql query (/showthread.php?tid=79710)



I need to get data from different table using sql query - shameelhp - 07-22-2021

Hi, Iam new to CI4 and have been exploring it for a couple of weeks now. I usually create an instance of Model in the Controller for performing Database operations. But now I need to get data from different table using sql query (for example: select user.*,country.countryname from user,country where user.country=country.countryid) Here the PK of Country table is stored in the 'country' field of user table, I need to get the corresponding country name from country table. This was easy by just executing the query in normal php. Since I'm new to CI4, a little in trouble with this. Any help would be very much appreciated.

Thanks in advance.


RE: I need to get data from different table using sql query - ikesela - 07-22-2021

$model is User table model.

$model->select(' user.*,country.countryname')->join('country','user.country=country.countryid','left')->findAll();


RE: I need to get data from different table using sql query - paliz - 07-22-2021

Join tables to gather