Jump to content

Recommended Posts

Hi all,

 

        I am experiencing a strange behaviour with my query

 

SELECT COUNT( * ) FROM cat_completiondate WHERE  `EmployeeNumber` =34550 which is returning '1'

 

  BUT my database has 2 rows for the EmployeeNumber=34550

 

My table cat_completiondate has a composite primary key with EmployeeNumber and Positionheld

 

Can anyone help me please !!

Link to comment
https://forums.phpfreaks.com/topic/181019-very-strange-behaviour-with-count-query/
Share on other sites

Hi Maq,

 

        I have two rows for the same employeenumber , my table contains EMPLOYEENUMBER with multiple job positions so my table looks something like this

 

  EmployeeNumber          JobPosition                CategoryNumber

    34550                        1                                1

    34550                        1                                2

    34550                        1                                5

 

        My intention was to count the number of rows for  EmployeeNumber for the each job position in this table so My intention result is 3 if go for 34550 and 1

 

 

Sure !!

 

    EmployeeNumber     Cat_Num CategoryJobPosition

34450 2                 1

34550 1                 1

My code is

 

    $sql = "SELECT count(*) as count FROM `cat_completiondate` WHERE `EmployeeNumber`=34550 and `CategoryJobPosition`=1";

$result=mysql_query($sql);

$row=mysql_fetch_array($result);

echo($row['count']);

 

 

OUTPUT is

1

 

     

 

Sorry !! I didnot display it correctly

 

EmployeeNumber        Cat_Num  CategoryJobPosition 

        34450                  2                  1     

        34550                  1                  1     

 

  This is my database !!!

It's also weird that you have a table called "cat_completiondate" with no dates...

 

Sorry !! I didnot display it correctly

 

EmployeeNumber        Cat_Num  CategoryJobPosition 

        34450                  2                  1     

        34550                  1                  1     

 

  This is my database !!!

 

They're still different.

Sorry !! I didnot display it correctly

 

EmployeeNumber        Cat_Num  CategoryJobPosition 

        34450                  2                  1     

        34550                  1                  1     

 

  This is my database !!!

 

First row: EmployeeNumber = 34450 - does not match your conditions

Second row: EmployeeNumber = 34550 - matches your conditions

Number of rows that match your conditions: 1

It has 2 rows for the WHERE CLAUSE, you can also see that in the database snapshot !!!

 

 

 

Maybe you're confused on what the logical operator "AND" means.  It means that SQL will only return results that match both of the conditions.  If you have two different ID's then how could they both possibly be returned?

Hi Maq,

 

        I have two rows for the same employeenumber , my table contains EMPLOYEENUMBER with multiple job positions so my table looks something like this

 

  EmployeeNumber          JobPosition                CategoryNumber

    34550                        1                                1

    34550                        1                                2

    34550                        1                                5

 

        My intention was to count the number of rows for  EmployeeNumber for the each job position in this table so My intention result is 3 if go for 34550 and 1

 

 

 

Ok, so now that the "mystery" is resolved, I will opin about what you really wanted all along.

 

SELECT EmployeeNumber, count(*) as countOf FROM cat_completiondate WHERE CategoryJobPosition = 1 GROUP BY EmployeeNumber, CategoryJobPosition

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.