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


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(*).

$query = $this->db
    ->select('COUNT(*) AS num_entries', FALSE)
    ->join('table_b AS TB', 'TB.id_category = TA.id', 'inner')
    ->get('table_a AS TA');

Thank You vey much. It works !!!

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');

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.