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

  • Home
  • SEARCH
  • 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 9020291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:58:52+00:00 2026-06-16T04:58:52+00:00

I have the following scheme: CREATE TABLE IF NOT EXISTS `answers` ( `id` bigint(20)

  • 0

I have the following scheme:

CREATE TABLE IF NOT EXISTS `answers` (
`id` bigint(20) unsigned NOT NULL,
`answer` varchar(200) NOT NULL,
`username` varchar(15) NOT NULL,
`date` datetime NOT NULL,
PRIMARY KEY (`id`,`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

INSERT INTO `answers` (`id`, `answer`, `username`, `date`) VALUES
(1, 'gfdsf', 'guy', '2012-12-22 00:00:00'),
(4, 'gfdddsfs', 'maricela', '2012-12-22 00:00:00'),
(4, 'gfddsfs', 'mikha', '2012-12-22 00:00:00'),
(4, 'gfdsfs', 'guy', '2012-12-22 00:00:00');

CREATE TABLE IF NOT EXISTS `questions` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`asker_username` varchar(15) NOT NULL,
`target_username` varchar(15) NOT NULL,
`question` varchar(200) NOT NULL,
`hide` enum('y','n') NOT NULL DEFAULT 'n',
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

INSERT INTO `questions` (`id`, `asker_username`, `target_username`, `question`, `date`) VALUES
(1, 'mikha', 'guy', 'testo festo', '2012-12-22 00:00:00'),
(2, 'mikha', 'guy', 'saaaaaaaar', '2012-12-22 00:00:00'),
(3, 'sys.tem', 'every.one', 'test g1', '2012-12-06 00:00:00'),
(4, 'sys.tem', 'every.one', 'test g2', '2012-12-06 00:00:00');

I use the following query:

   SELECT        
   questions.id AS questionid,
   COUNT(answers.username) AS count_everyone,
   answers.username  
   FROM questions
   LEFT JOIN answers ON questions.id = answers.id
   GROUP BY questions.id,answers.username

The problem is with the COUNT(answers.username. I want to count the answers for each question but the query displays the count as 1. For example the question ID 4 is answered 3 times but the COUNT(answers.username) displays it as 1 instead of 3.

This is the expected result:

         questionid count_everyone  username
               1         1            guy
               2         0            null
               3         0            null
               4         3             guy
               4         3           maricela
               4         3             mikha

This is the result I actually get:

         questionid count_everyone  username
               1         1            guy
               2         0            null
               3         0            null
               4         1             guy
               4         1           maricela
               4         1             mikha

Thanks

  • 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-06-16T04:58:53+00:00Added an answer on June 16, 2026 at 4:58 am

    To get the correct count you should only group by the question id, but not the username:

    SELECT        
        questions.id AS questionid,
        COUNT(answers.username) AS count_everyone
    FROM questions
    LEFT JOIN answers ON questions.id = answers.id
    GROUP BY questions.id
    

    If you have to get the usernames in the same query then use a join:

    SELECT questionid, count_everyone, username
    FROM
    (
        SELECT        
            questions.id AS questionid,
            COUNT(answers.username) AS count_everyone
        FROM questions
        LEFT JOIN answers ON questions.id = answers.id
        GROUP BY questions.id
    ) T1
    LEFT JOIN answers ON T1.questionid = answers.id
    

    sqlfiddle

    or GROUP_CONCAT:

    SELECT        
        questions.id AS questionid,
        COUNT(answers.username) AS count_everyone,
        GROUP_CONCAT(answers.username) AS usernames
    FROM questions
    LEFT JOIN answers ON questions.id = answers.id
    GROUP BY questions.id
    

    sqlfiddle

    Or a correlated subquery:

    SELECT        
        questions.id AS questionid,
        (SELECT COUNT(*) FROM answers WHERE questions.id = answers.id) AS count_everyone,
        answers.username
    FROM questions
    LEFT JOIN answers ON questions.id = answers.id
    

    sqlfiddle

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following scheme: CREATE TABLE IF NOT EXISTS `connections` ( `username1` varchar(15)
I have a table with the following schema: CREATE TABLE IF NOT EXISTS `feeds`
I have the following table schema: CREATE TABLE [Foo]( [id] [int] NOT NULL, [name]
I have two tables with the following schema: CREATE TABLE sales_data ( sales_time date
I have the following schema: Database: test. Table: per_login_user, Field: username (PK), password Database:
I have the following schema, which I've simplified slightly: CREATE TABLE [dbo].[Header] ( [HeaderId]
I have the following table in my iSeries DB2 schema: CREATE TABLE LATHAM1.SGINES1.LICVP010 (ID
I have 3 tables with the following schema: CREATE TABLE `devices` ( `device_id` int(11)
I have a pretty simple table in SQLite, with the following schema: CREATE TABLE
I have the following pseudo-SQL schema: table flight id int primary key date timestamp

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.