Welcome Guest, Not a member yet? Register   Sign In
solved: running into small problems with complicated queries
#1

[eluser]Vitsaus[/eluser]
Once again with stupid questions.

I'm running into difficulties with CI's way to produce queries from database. I have a following query and I have no idea how to convert into CI world:

Code:
SELECT pages.id, pages.title, pages.shortly, pages.content, pages.last_edit_time, pages.last_editor, pages.url, customers.first_name, customers.last_name
FROM pages
WHERE pages.id = '$id'
INNER JOIN customers
ON pages.last_editor_id = customers.id

This is what I tried when reading documentation but there is obviously problems with the way I give names to my tables in MySQL database.

Code:
// Notice customers.first_name, customers.last_name in the end of the first line. Something wrong with that naming convention?
$this->db->select('pages.id, pages.title, pages.shortly, pages.content, pages.last_edit_time, pages.last_editor, pages.url, customers.first_name, customers.last_name');
$this->db->from('pages');
$this->db->join('customers','pages.last_editor = customer.id');
$this->db->where('pages',$id);
$db = $this->db->get();

This try just produces following error:


Error Number: 1054

Unknown column 'pages' in 'where clause'

SELECT `pages`.`id`, `pages`.`title`, `pages`.`shortly`, `pages`.`content`, `pages`.`last_edit_time`, `pages`.`last_editor`, `pages`.`url`, `customers`.`first_name`, `customers`.`last_name` FROM (`pages`) JOIN `customers` ON `pages`.`last_editor` = `customer`.`id` WHERE `pages` = 2

So, the resultinq query is obviously not what I want. Is there any clearer documention on using joins and making bit more complicated SQL queries with CI?

Edit, fixed by myself,just some stupid typos etc.
#2

[eluser]Aniket[/eluser]
try providing second parameter to the $this->db->join() method.
like : $this->db->join('customers','pages.last_editor = customer.id','inner');
If you need something other than a natural JOIN you can specify it via the third parameter of the function. Options are: left, right, outer, inner, left outer, and right outer.




Theme © iAndrew 2016 - Forum software by © MyBB