在PHP中使用MAX和SUBSTRING_INDEX的SQL查询

How to execute a complex query in drupal

I tried :

 $sql = "SELECT  MAX(SUBSTRING_INDEX(`table_field1`, '/', -1)) as mm FROM `tableitems` WHERE table_field2=' " .$selValue."'";
 $query = db_query($sql);
 echo $query;

It returns Array as the echo output.

Can someone please guide me through this. I just wanted the maximum value from the column in my database. And Iam getting the correct value when i tried in MYSQL directly.

Also i tried:

 $sql = "SELECT  MAX(SUBSTRING_INDEX(`table_field1`, '/', -1)) as mm FROM `tableitems` WHERE fieldprefix=' " .$selValue."'";
 $query = db_query($sql);
 foreach($query as $r)
  echo $r->mm;

But still it gives incorrect

You have to use like below to fetch the field value in drupal

$sql = "SELECT  MAX(SUBSTRING_INDEX(`table_field1`, '/', -1)) as mm FROM `tableitems` WHERE table_field2=' " .$selValue."'";
$query = db_query($sql)->fetchField();
print_r($query);