Mysql query for count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Noorain
    New Member
    • Mar 2008
    • 57

    #1

    Mysql query for count

    sir

    i want a query which count total student. here 6 record of student. here one student two times entry. my result is total student 5. i can't do this.

    my table is:
    id stu_name dob
    1 Saima 2009-02-01
    2 Tanvir 2009-02-11
    3 Saima 2009-02-01
    4 Rumman 2009-02-01
    5 Saima 2009-02-26
    6 Saima 2009-02-27

    i use this query
    Code:
    select count(e.stu_name) as 'Total Number' from
    stu_info e,stu_info d,stu_info c
    where (e.stu_name <> d.stu_name
    and e.dob = d.dob)
    but it doesn't show proper result. please help me

    Thanks
  • mwasif
    Recognized Expert Contributor
    • Jul 2006
    • 802

    #2
    You need a GROUP BY on name or DISTINCT

    [CODE=mysql]select count(*) as 'Total Number' from
    stu_info e,stu_info d,stu_info c
    where (e.stu_name <> d.stu_name
    and e.dob = d.dob)
    GROUP BY e.stu_name[/CODE]

    But in your case, you have different dob for students. This mean, some students with the same name may be different.

    Use this code
    [CODE=mysql]SELECT COUNT(*) as 'Total Number' FROM (
    select count(DISTINCT e.stu_name) from
    stu_info e,stu_info d,stu_info c
    where (e.stu_name <> d.stu_name
    and e.dob = d.dob)
    ) tmp
    [/CODE]

    Comment

    Working...