Welcome Guest, Not a member yet? Register   Sign In
Muliple table joining with conditional statements
#1

[eluser]sazet_rey[/eluser]
i want to run a query in codeigniter;

the query is

"SELECT * FROM td_album,td_image
WHERE td_album.album_id=td_image.album_id
AND td_album.album_id='$id'
AND (td_image.image_name='$name' OR td_image.image_id='$image_id');"



i wrote this codeigniter query

$query = $this->db->select('*')
->from('td_image')
->join('td_album', 'td_album.album_id = td_image.album_id')
->like('td_image.image_id', $descp)
->or_like('td_image.image_name', $descp)
->like('td_album.album_id',$a_id)
->get('', $this->limit, $this->offset);




But the problem is that, if i have two albums with id 1 and 2, and i want to show images of album 1 only,

this codeigniter query is returning result of al the images, i.e. displaying images of both the albums, which should not happen.

Actualy it should display images of only that album whose album_id is 1


now i realise the above codeigniter query stands for

"SELECT * FROM td_album,td_image
WHERE td_album.album_id=td_image.album_id
AND td_album.album_id LIKE'$id'
OR td_image.image_name LIKE'$name' AND td_image.image_id LIKE'$image_id';"


and so it displays all the images, but my requirement is to display images of only one album

so, i need codeigniter query to be equivalent to this mysql query

"SELECT * FROM td_album,td_image
WHERE td_album.album_id=td_image.album_id
AND td_album.album_id LIKE'$id'
AND (td_image.image_name LIKE'$name' OR td_image.image_id LIKE'$image_id');"


Can anybody help me converting the last query to codeigniter query???

Thanks in advance.

A fruitfull answer is appreciated, not a non-tested or assumed answer.
#2

[eluser]Harold Villacorte[/eluser]
I would take a stab at this, but I cannot guarantee it would work. I can only guarantee that it would be 100% untested and totally assumed. And it would help to know what fields to select and from which table.
#3

[eluser]Aken[/eluser]
Same problem.
#4

[eluser]Unknown[/eluser]
$query = $this->db->select('album.*, image.*')
->from('td_album, td_image')
->join('td_image', 'td_image.album_id =td_ album.id', 'left')
->group_by('album.id');

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




Theme © iAndrew 2016 - Forum software by © MyBB