Hello,
I thought I would ask in the PHP forum, if you know of a way to limit the first query in a LEFT JOIN to one record. This to help answer this question.
I’ve come up with this query.
$sql = "SELECT
b2.gebruiker_id,
b2.bericht,
b2.verzonden
FROM berichten AS b1
LEFT JOIN berichten AS b2
ON b2.gebruiker_id = b1.gebruiker_id
WHERE b1.isNieuw = 1";
However, it will return ALL records from the first query where isNieuw = 1.
SO we do want all matching results from the JOIN query but only one result from the first query so we can’t use LIMIT 1.
Any ideas?
It can be done in two looped queries.
It could be weeded out building a data array with the results.
Just trying to see if it can be done in a single query.
SELECT gebruiker_id
, bericht
, verzonden
FROM berichten
WHERE gebruiker_id =
( SELECT gebruiker_id
FROM berichten
WHERE verzonden =
( SELECT MIN(verzonden)
FROM berichten
WHERE isNieuw = 1 ) )
Not if you put it in a subquery, then it just limits the subquery.
This is basically what r937’s answer also does, except he uses an implicit limit using the fact that the MIN function will only return one value.