Welcome Guest, Not a member yet? Register   Sign In
Active records and complex join query
#1

[eluser]softsaurus[/eluser]
I hope somebody here can help me out with this issue I'm having, I would be forever greatful!
I'm having great difficulty getting my query to run right.
I'm trying to join the following tables:

Code:
- entries (id, title, body...)
- images (id, title, description, src...)
- images_entries (id, image_id, entry_id)(which of course contains the corresponding relationships).

What I'd like my query to do is be able to get the fields contained in 'images' and link them to the entry on my 'blog' by using the relational table.

It's also good to know that my knowledge on relational databases and using joins is quite slim.. have been studying these darn queries for days now but I just can't wrap my head around this one...

This is what I got so far..
Code:
SELECT `Entry`.`id`, `Entry`.`title`, `Entry`.`body`, `ImgEnt`.`id`, `ImgEnt`.`entry_id`, `ImgEnt`.`image_id` FROM `entries` AS `Entry` JOIN `images_entries` AS `ImgEnt` ON (`ImgEnt`.`image_id` = 1 AND `ImgEnt`.`image_id` = `Entry`.`id`)

This however results in the joining of the images_entries table but not being able to call upon the images table and it's content. So my guess is that another join should be made somewhere.. but I'm clueless as to where and how..

I also translated the query above to an 'active record' CI style query but without having the knowledge as to where and how to join the second table that's pretty useless..
But for the record, this is what I came up with:
Code:
$this->db->select('entries.id, entries.title, entries.body');
$this->db->from('entries');
$this->db->join('images_entries', 'entries.id = images_entries.entry_id');
$q = $this->db->get();

If one of you could help me out that would be awesome Smile

Thanks in advance guys, this thing has been bugging me for months now! Tongue FAIL


Messages In This Thread
Active records and complex join query - by El Forum - 05-20-2010, 06:40 AM
Active records and complex join query - by El Forum - 05-20-2010, 08:35 AM
Active records and complex join query - by El Forum - 05-20-2010, 09:57 AM
Active records and complex join query - by El Forum - 05-20-2010, 11:33 AM
Active records and complex join query - by El Forum - 06-09-2010, 02:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB