Welcome Guest, Not a member yet? Register   Sign In
How do I do a select as for a join statement?
#1

[eluser]Zac G.[/eluser]
Hi, I am trying to do a join statement using CI's active record and I want to get the id column of the original table I'm querying, but it keeps giving me the second table's id instead.

Here is my code:

Code:
$this->db->where('courses.id', $course_id);
$this->db->join('course_programs', 'courses.id = course_programs.course_id');
$this->db->join('course_start_terms', 'courses.id = course_start_terms.course_id');
$query = $this->db->get('courses');
foreach ($query->result() as $row):
  $course['id'] = $row->id;
  $course['code'] = $row->code;
  //More code...
endforeach;

Any suggestions on how I would get the courses.id?

Thanks!
#2

[eluser]überfuzz[/eluser]
Specify what you want.

Code:
$this->db->select('table_name.column_name AS id');
#3

[eluser]sophistry[/eluser]
+1 uberfuzz's suggestion.

i might change it to something more explicit:
Code:
$this->db->select('courses.id AS courses_id');

just to clarify, this is not a problem with the way CI's active record works... it is how sql works.
#4

[eluser]Zac G.[/eluser]
Thanks folks! That's what I figured, I just didn't want want to have to write out everything that I'm selecting :p
#5

[eluser]Phil Sturgeon[/eluser]
You can do this:

Code:
$this->db->select('courses.id AS courses_id');
$this->db->select('course_programs.*, course_start_terms.*');
#6

[eluser]Zac G.[/eluser]
@phil,

Now THATs what I'm talking about!!!

Sweet, sweet, sql




Theme © iAndrew 2016 - Forum software by © MyBB