• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with database query

#1
[eluser]dagon[/eluser]
Hello

Im quite new to code igniter and the concept of active record pattern of database handling. I have problems with translating such SQL query into a series of active record method calls:

select TA.*, count(*) as num_entries from table_a AS TA inner join table_b AS TB ON TB.id_category = TA.id GROUP BY TB.id_category ORDER BY num_entries DESC;

Can anyone help ? I have aspecially problems with adapting count(*).

#2
[eluser]danmontgomery[/eluser]
Code:
$query = $this->db
    ->select('TA.*')
    ->select('COUNT(*) AS num_entries', FALSE)
    ->join('table_b AS TB', 'TB.id_category = TA.id', 'inner')
    ->group_by('TB.id_category')
    ->order_by('num_entries')
    ->get('table_a AS TA');

#3
[eluser]dagon[/eluser]
Thank You vey much. It works !!!

#4
[eluser]dagon[/eluser]
It seems that I ran onto another obstacle. Im trying to delete data from two tables using join(). but $this->db->delete() seems to take into consideration only previous declarations of where(), any joins are ommited. The query which I want to perform is:

DELETE TA,TB FROM table_a AS TA LEFT JOIN table_b AS TB ON TA.id = TB.other_id WHERE TA.field = 'value';

I try to solve it like this:

$this->db->from('table_a AS TA');
$this->db->where('TA.field', $value);
$this->db->join('table_b AS TB', 'TA.id = TB.other_id', 'left');
$this->db->delete();

It seems that the above code does not generate the "JOIN" part of the query. Anyone know why ? Maybe this is a bug ?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.