Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6370023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:52:40+00:00 2026-05-25T00:52:40+00:00

Hey here is the SQL statement I am using $sql = SELECT number, COUNT(name)

  • 0

Hey here is the SQL statement I am using

$sql = "SELECT number, COUNT(name) FROM people WHERE id='$id' GROUP BY number";  
$result = mysql_query($sql);

while($row = mysql_fetch_array($result)){
$statarray['stat'.$i] = $row['COUNT(name)'];
$i++; }

The number column stores a 0, 1, 2 or a 3 against each name so the array comes back totalling how many names there are against each number.. which works fine.

The only problem is, if all the names have a number 1 stored against them, my array entry ‘stat0’ does not tell me how many 0s there are because it is replaced by the number of 1s (this is because there are no 0s in the array so the first output is 1s)..

How do I find out how many names have 0s 1s 2s and 3s in their number column including when there are none?.. thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:52:41+00:00Added an answer on May 25, 2026 at 12:52 am
    SELECT  numbers.number ,
            COUNT(name)
    FROM    ( SELECT    0 AS number
              UNION ALL
              SELECT    1
              UNION ALL
              SELECT    2
              UNION ALL
              SELECT    3
            ) numbers
            LEFT JOIN people ON people.number = numbers.number AND id='$id' 
    GROUP BY numbers.number
    

    Or if you have a permanent numbers table as per comments

    SELECT  numbers.number ,
            COUNT(name)
    FROM    numbers
            LEFT JOIN people ON people.number = numbers.number
                                AND id = '$id'
    WHERE   numbers.number BETWEEN 0 AND 3
    GROUP BY numbers.number
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys, here is my code for this, the only help i get from
Hey folks, I am trying to import a database (.sql) file using phpmy admin
Hey all here is one question from Gate exam which i found a bit
Hey I got a problem here. I'm trying to retrieve the date from the
Hey I have SQL writers block. So here is what I'm trying to do
Hey, a beginner's question here.... In order to learn PHP and SQl I have
Hey, I'm pretty new to linux (using Ubuntu 11.04) so bear with me here.
Hey! I am not trying to push my luck here but I have another
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.