Welcome Guest, Not a member yet? Register   Sign In
MySql joins
#1

[eluser]Todlerone[/eluser]
Hello all and TY in advance. How do you join three tables together in a query? I have three tables: schedule, demographics and treatments. I want to join several rows from the schedule and demographics tables to the treatment table.

From model
Code:
function display_planning(){
        $this->db->select('*');
        $this->db->from('demographics');
        $this->db->join('treatments','demographics.demo_NumID = treatments.demo_NumID');
        $query = $this->db->get();
        return $query->result_array();
    }

from controller
Code:
$data['planning'] = $this->Patients->display_planning();
Not sure how to put the query and join in from my schedule table.

CHEERS
#2

[eluser]xatrix[/eluser]
http://www.w3schools.com/sql/sql_join_left.asp
#3

[eluser]Todlerone[/eluser]
[quote author="xatrix" date="1292901446"]http://www.w3schools.com/sql/sql_join_left.asp[/quote]

TY but I know how joins work, have used many in my website. I have never done or needed to join three together before now. Just not sure of the CI nomenclature. The user quide says:
Quote:Multiple function calls can be made if you need several joins in one query.

Not sure what they're eluding to.

TY
#4

[eluser]xatrix[/eluser]
Call db->join() as many times as you need. For example if you've got 3 tables:

Code:
$this->db->select('demographics.*');
$this->db->from('demographics');
$this->db->join('treatments','demographics.demo_NumID = treatments.demo_NumID');
$this->db->join('schedule','demographics.demo_NumID = schedule.demo_NumID');

Also do note, that if you're joining everything in these tables, then columns that have the same name will overwrite, which will lead to ambiguous results. Better to select only specific columns.
#5

[eluser]Todlerone[/eluser]
TY xatrix, I'll give it a try.
#6

[eluser]Todlerone[/eluser]
Worked perfectly...TYTY




Theme © iAndrew 2016 - Forum software by © MyBB