This query returns the data I require given a specific traceid
value (50 in this example, which is a key that exists in both tables, tblResults
and tblTraces
:
SELECT count(changed)
FROM (
SELECT changed
FROM tblResults
WHERE traceid = 50
AND changed = 1
ORDER BY resultid DESC
LIMIT 0,20
) as R
I want to run this above query against nearly every traceid
(so, selecting 20 rows for each traceid, not just traceid 50, so 3 traceid's would mean 60 rows). This simple select below gets the required traceid
values:
SELECT `traceid` FROM `tblTraces` WHERE `enabled` = 1
How can I "glue" the two queries together?
So I imagine a query like the following, except it won't work because the subquery is returning multiple rows (which I want):
SELECT count(changed)
FROM (
SELECT changed
FROM tblResults
WHERE traceid = (
SELECT `traceid` FROM `tblTraces` WHERE `enabled` = 1
)
AND changed = 1
ORDER BY resultid DESC
LIMIT 0,20
) as R