⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

How to get max id in join?



deib97
  • profile picture
  • Member

Posted 06 April 2013 - 03:56 AM

I have a problem with my model. i have query like this :

SELECT a.id_stap, a.kd_stap, a.stap, b.nil_punish, b.tgl_punish
FROM tbl_status a
LEFT
JOIN tbl_punish b ON a.id_stap = b.kd_stat_punish
AND b.id_i_punish = (
SELECT max( id_i_punish )
FROM tbl_punish
WHERE kd_stat_punish = a.id_stap ) 

and my model :

 

function get_list()
	    {
		 if($this->table_name === null)
		  return false;
	    
		 $select = "{$this->table_name}.*";
	  
  $select .= ", tbl_punish.tgl_punish, tbl_punish.nil_punish";
 
		 if(!empty($this->relation))
		  foreach($this->relation as $relation)
		  {
		   list($field_name , $related_table , $related_field_title) = $relation;
		   $unique_join_name = $this->_unique_join_name($field_name);
		   $unique_field_name = $this->_unique_field_name($field_name);
		  
	    if(strstr($related_field_title,'{'))
			    $select .= ", CONCAT('".str_replace(array('{','}'),array("',COALESCE({$unique_join_name}.",", ''),'"),str_replace("'","\\'",$related_field_title))."') as $unique_field_name";
		   else   
			    $select .= ", $unique_join_name.$related_field_title as $unique_field_name";
		  
		   if($this->field_exists($related_field_title))
			    $select .= ", {$this->table_name}.$related_field_title as '{$this->table_name}.$related_field_title'";
		  }
	      
		 $this->db->select($select, false);
		 $this->db->join('tbl_punish','tbl_punish.kd_stat_punish = '. $this->table_name . '.id_stap','left');
		 $this->db->group_by('kd_stap');
		 $results = $this->db->get($this->table_name)->result();
	    
		 return $results;
	    }
           }

how to i write a code in mymodel?:

AND b.id_i_punish = (
SELECT max( id_i_punish )
FROM tbl_punish
WHERE kd_stat_punish = a.id_stap ) 

can any ide???


davidoster
  • profile picture
  • Member

Posted 06 April 2013 - 05:41 AM

http://stackoverflow.com/questions/9144677/left-join-on-max-value


deib97
  • profile picture
  • Member

Posted 06 April 2013 - 07:20 AM

thanks davidoster ,

but how i can combine code in my custom model in grocery crud?,

Im try to get id from every row, to make a function in my controller but can't show the result. :(


davidoster
  • profile picture
  • Member

Posted 06 April 2013 - 07:44 AM

Hmm! Sorry I got it all wrong before.

 

I am still trying to understand what you're trying to achieve.

Where/when exactly do you want to get this id? From the Grocery CRUD's list? From your model?

And when you get this id what do you want to do with it?

 

For example, usually what we do is to call one of the callback functions, while in grid mode, from our controller, which gets some row data and we call a model function that returns some other data and finally display to the user a changed field depending on the logic.

Is this what you are trying to do?


deib97
  • profile picture
  • Member

Posted 08 April 2013 - 01:41 AM

thanks davidoster, for your reply... :)

i'll try your ide..done david..

 

custom model, and make callback with set_primary_key.. and show the result. thanks :D