I have 2 tables:
attribuut
attribuutvalue
They have a one-to-many relationship. An attribuut can have multiple attribuutvalues. These attribuutvalues contain states.
Now I want a query which gives me back the latest attribuutvalue from an attribuut, which has: state 3 or state 6.
Then i hit my problem: When an attribuut contains an attribuutvalue with state 4, only the latest attribuutvalue with state 3 should be shown.
SELECT DISTINCT * FROM attribuut as att
LEFT JOIN attribuutvalue as value ON (value.attribuuthead = att.displayid)
WHERE value.status = 3 OR (value.status = 6
AND NOT EXISTS
(SELECT * FROM attribuutvalue as value2 WHERE value2.valueid = value.valueid AND value2.status = 4))
ORDER BY valueid DESC
However this gives me not the resultset i want. There are still attribuutvalues with state 4 shown. And it doesnt give me only the last record in the list...