There is a database and a table. It contains many meanings.

I need to output all table values ​​in json format and another additional condition, if the rows are the same (but the cost parameter is different, then output the largest of them).

Here is my code.

mysql_connect($sdd_db_host,$sdd_db_user,$sdd_db_pass) or die(mysql_error()); mysql_select_db($sdd_db_name) or die(mysql_error()); mysql_set_charset('utf8'); $date = array(); $row = mysql_query("SELECT sh,name,ed,MAX(cost) AS cost FROM 'datatable'"); while($row = mysql_fetch_assoc($row){ $data[] = $row; } echo json_encode($data); 

?>

What is the problem?

    1 answer 1

    The point is in the aggregating function max (), with it you need to use grouping.

    • You did not answer the question. - Martinez Toni
    • 2
      @MartinezToni, very interesting, what is the answer you need? When using max, the data needs to be grouped (group by), and I am not a telepath to understand by what field there should be grouped. - Zhandos Man pm
    • @MartinezToni GROUP BY 'name' - E_p 2:21 pm