0

I need to group some data from my DB, say if my data is

url, siteId
abc, 1
abc, 1
abc, 2
abc, 2
abc, 2

I need to count the number of URL's at each site. I tried using GROUP BY url and SELECT *, COUNT(url) as urlCount, however, that grouped all 5 together. Is this possible using mysql alone or would I need some PHP to format that data?

1
  • You want to GROUP BY url then it will show only 1 row as abc is the only value of the url column. Please clarify what you want. Commented Nov 26, 2013 at 11:46

2 Answers 2

1

Group by siteidand not by url

select siteid, count(distinct url) as urlCount
from your_table
group by siteid

If you use distinct then you will get the count of unique urls.

Sign up to request clarification or add additional context in comments.

1 Comment

This worked perfectly, I tried that sql before, but without the distinct, thanks! I'll accept this soon as SO lets me.
1

This should work:

SELECT siteId, COUNT(url) AS urlCount
FROM tablename
GROUP BY siteId

As you wrote, I need to count the number of URL's at each site, for sql query you would write this as GROUP BY siteId.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.