0

Ok, maybe to many hours the last couple of days but I am unable to fix this issue.

I have this table:

With this query I am trying to find all status of each artikel_inh_id with the latest date

SELECT
MAX(stat.datum) AS date,
stat.artikel_inh_id AS artikel_inh_id,
stat.status AS status
FROM verh_artikel_ihn AS art
LEFT JOIN verh_artikel_status AS stat
ON stat.artikel_inh_id = art.id
WHERE art.artikel_id = ".$row_table_2['id']."
GROUP BY stat.artikel_inh_id

For some reason all results are giving status = Beheerder although the lasted date is shown. Any suggestions?

Muiter
  • 1,470
  • 6
  • 26
  • 39

1 Answers1

0

Seems like you missed adding "status" column in the group by. When you have an aggregation function in the select of any SQL query along with other columns you need to group by all the columns on which you are not doing the aggregation.

In Mysql, this missing column in group by is not treated as an error, instead it give you the first value it comes across for the column in all the rows on which its aggregating. That's why you got the above mentioned result.

Here's the documentation for the same: https://dev.mysql.com/doc/refman/5.7/en/aggregate-functions.html

This might help too: https://www.w3schools.com/sql/sql_groupby.asp