I need a user-specific mySQL SELECT of the last n entries within n different topics.
I have two tables: areas and posts.
areas has the columns id, coordinates, userID
posts has the columns iD, tOPIC, cONTENT, aREAid, cREATED
with the value of the aREAid in each row is the corresponding id of the area table. This means the chain between the two tables is: id <=> aREAid
user-specific query in table "areas":
$go4Row1 = $pdo->prepare ('SELECT id,coordinates,userID FROM areas WHERE userID = ?');
$go4Row1->bindParam(1, $gottenUsersID, PDO::PARAM_INT);
$go4Row1->execute();
to show the last 15 entries within 15 different topics in table "posts":
$latest_n_distinct_posts =
$pdo->prepare('SELECT MAX(cREATED) AS max_date, iD, tOPIC, cONTENT, aREAid, cREATED
FROM posts
GROUP BY
tOPIC
ORDER BY
max_date DESC
LIMIT 15');
// the WHERE-clause was no solution here
// ...$latest_n_distinct_posts->bindParam(1, $aREAid, PDO::PARAM_INT);
$latest_n_distinct_posts->execute();
I can do a functioning:
while ($areaRow = $go4Row1->fetch(PDO::FETCH_ASSOC)) {
echo' show: '.$areaRow['id'].' with coordinates:
'.$areaRow['coordinates'].' userID: '.$areaRow['userID'].'<br>';
}
or a functioning:
while ($latestRow = $latest_n_distinct_posts->fetch(PDO::FETCH_ASSOC)) {
echo' last date: '.$latestRow['cREATED'].' with topic:
'.$latestRow['tOPIC'].' text: '.$latestRow['cONTENT'].'<br>';
}
But I fail in making the posts query user-specific by finding a working solution in chaining the restriction WHERE id <=> aREAid somehow. I am not sure if it is necessary to create an array with the aREAid_s and use an IN clause for generating the needed "bloc"/ "filtered population" from which I can use the GROUP BY / ORDER BY / LIMIT then. (But I would appreciate a simpler solution...)
I'd be really happy if someone could give me a hint / help me out with this issue.
Thanx in advance, -Flow
p.s. did change the column-name from tEXT to cONTENT - to avoid using a keyword (here is a keyword-list, one might be interested. more words then expected: https://dev.mysql.com/doc/refman/5.7/en/keywords.html )
p.p.s. this is an example-code. the origin has different column-names and each query for itself is working properly..