This is a discussion on "Please Help with (should be) simple count() query." within the Databases section. This forum, and the thread "Please Help with (should be) simple count() query. are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Please Help with (should be) simple count() query.
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
#1
|
|||
|
|||
|
Please Help with (should be) simple count() query.
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! |
|
|
|
#2
|
|||
|
|||
|
Re: Please Help with (should be) simple count() query.
Ahhh, I think I figured it out... Is it because I'm selecting FROM the taglink table and not selecting directly from the query that is nested? I'm rusty...
|
|
#3
|
|||
|
|||
|
Re: Please Help with (should be) simple count() query.
Yes... How do I craft the query to select FROM the rows in the subquery and not from the table 'taglink'
A appreciate the help! |
![]() |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Simple SQL Select Query Question | MrQuestions | Databases | 7 | Mar 20th, 2008 23:11 |
| Just want simple Background Song (.mp3) and a simple start/stop. | BlackReef | Flash & Multimedia Forum | 4 | Jun 28th, 2007 07:04 |
| MySQL query query | dangergeek | Databases | 3 | Apr 12th, 2007 12:45 |