I have a table with 3 columns
---QID---TEXT---CID---
I would like to find 20 rows(QID and TEXT) for each distinct CID. I have already prepared string $cid so that I can use WHERE IN statement.
SELECT * FROM questions q1
WHERE cid=(SELECT cid
FROM questions q2
WHERE q2.cid IN ($cids)
GROUP BY q2.cid)
ORDER BY q1.qid LIMIT 20
Thank you!
Simple query:
or, if
$cidis an array:To get to the results:
For more info on what you can do with the PDO object, refer to the manual
A quick fix (but not a good one) might be:
In the case of
CID IN(1,2,3), I’m not sure if there’s a strait forward way of doing this. All I can think of is using unions. Mayby this page can help you out with that.A fugly fix might also be to
ORDER BY CID ASC, and insted of usingfetchAll(), do this:This way, the
$resultsarray, will have a key for eachCIDthat was found, and that key’s value will be an array of up to 20 records…