简体   繁体   中英

How do you make a codeigniter join?

I'm having trouble creating a join in the codeigniter format, i have a MySQL query that returns what i want:

SELECT nwsite.siteid, nwsite.installer_username,
       calcdata.esolar, calcdata.time, calcdata.wsolar
FROM nwsite, calcdata
WHERE nwsite.siteid = calcdata.siteid AND time = '2011-10-29 12:45:00';

I've looked at the documentation but i keep getting it wrong.

Thanks

From the CI docs...

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

$query = $this->db->get();

// Produces: 
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id

So in your case, I would not use the cartisian product in your FROM , I would actually use a join.

$this->db->select("... your fields here...");
$this->db->from("nwsite");
$this->db->join("calcdata", "nwsite.siteid = calcdata.siteid");
$this->db->where("nwsite.time", "2011-10-29 12:45:00");

$query = $this->db->get();

For Future reference, I often utilize

die($this->db->last_query());

to output the actual query that's being generated. It makes it easier to debug and compare to what you are attempting to generate.

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