Hello,
I'm very rusty with my
SQL (as anyone reading my last post may remember), and could use a little assistance on building this query. Thanks for taking the time to help me. It should be a relatively simple Count() query but I'm not getting the right results for some reason and I'm hoping somebody can point out my error to me.
I'm attempting to count the number of times each m_id is returned after running this query:
SELECT m_id
FROM taglink
WHERE m_id <> '25'
AND t_id
IN (
SELECT t_id
FROM taglink
WHERE m_id = '25'
)
which returns:
m_id
33
34
34
35
35
35
36
I want to count the number of times that m_id is returned so that the results of my query will be:
m_id | count
33 | 1
34 | 2
35 | 3
36 | 1
In my attempt to do this, I run the following query:
SELECT m_id, count( m_id ) AS "count"
FROM taglink
WHERE m_id
IN (
SELECT m_id
FROM taglink
WHERE m_id <> '25'
AND t_id
IN (
SELECT t_id
FROM taglink
WHERE m_id = '25'
)
)
GROUP BY m_id
The problem I'm having is that the query returns:
m_id | count
33 | 3
34 | 2
35 | 3
36 | 3
It seems to return the count of "3" for fields that should count "1," but count correctly if the fields count "2" or "3."
Perhaps somebody can spot my error... I can't seem to wrap my brain around this one. Thanks so much for your time!