0

I know this topic has been discussed million times, but I am having a strange output. I am trying to accomplish a job where a sql query will count the number of duplicate + non-duplicate rows. Basically I have the following table:

ID 
865      
501     
501     
501
502
865
865

My query is

select id, count(*) as total from master_huts group by id

And I am getting this

ID     Name
0      (some weird number)
501    3
502    1
865    2

It's pretty straight forward, but not sure where I am wrong.

Table structure

CREATE TABLE `master_huts` (
 `hutids` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
10
  • Check for empty rows in table Commented Jun 19, 2014 at 12:44
  • @echo_Me The problem is ^ Commented Jun 19, 2014 at 12:45
  • @user3671491 how is defined your id column ? int or varchar ? Commented Jun 19, 2014 at 12:47
  • you sure you dont have 0 in your table or someother values ? look demo here it works fine sqlfiddle.com/#!2/f7195/1 Commented Jun 19, 2014 at 12:50
  • 1
    if you have large table ,try do this to see if you have 0 value select id from yourtable where id = 0 Commented Jun 19, 2014 at 12:54

3 Answers 3

1

Do the following:

select count(id) as ocurences, id from master_huts group by id
Sign up to request clarification or add additional context in comments.

2 Comments

It means you really have a row with id=0
My mistake as usual. Thanks mate.
1

Just use id in count instead of *

like this...

select id, count(id) as total from whatever group by id

3 Comments

Did that but still I am getting a 0 as first row
Are you sure your table has exact the same data given by you?
Solved :). my mistake. Sorry
1

try this and see if you still get anything weird:

select id, count(*) as total from master_huts where id != 0 group by id;

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.