Please Help with (should be) simple count() query.

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.



 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > Databases

Notices


Reply
 
LinkBack Thread Tools
  #1  
Old Mar 23rd, 2008, 19:43
New Member
Join Date: Mar 2008
Location: states
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Mar 23rd, 2008, 19:50
New Member
Join Date: Mar 2008
Location: states
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Mar 23rd, 2008, 20:42
New Member
Join Date: Mar 2008
Location: states
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 03:32.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42