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 7756623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:52:15+00:00 2026-06-01T12:52:15+00:00

So I have this table in my database Item Cat1 Cat2 ——————– Aaa Red

  • 0

So I have this table in my database

Item    Cat1    Cat2
--------------------
Aaa     Red     Used
Aaa     Blu     Used
Bbb     Gre     New
Bbb     Blu     New
Ccc     Gre     New
Ddd     Blu     Used

and I want to shows how many Items are Red in one column and New in an other column, like this:

Item    Red     New
-------------------
Aaa     1       0
Bbb     0       2
Ccc     0       1
Ddd     0       0

I know how to display them in two tables, but don’t know how to combine them.

$query = mysql_query("SELECT *, count(Item) AS CountItem FROM Table WHERE Cat1 = 'Red' GROUP BY Item");
$query2 = mysql_query("SELECT *, count(Item) AS CountItem2 FROM Table WHERE Cat2 = 'New' GROUP BY Item");

while($row = mysql_fetch_array($query) AND $row2 = mysql_fetch_array($query2))
  {
  echo $row['CountItem'] . " " . $row2['CountItem2'] . " " . $row['Item'];
  echo "<br>";
  }

This doesn’t seem to work as this only shows Items that are labeled Aaa and I’m having trouble understanding what I am doing wrong here.

  • 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-01T12:52:16+00:00Added an answer on June 1, 2026 at 12:52 pm

    This is a job for SUM(CASE):

    SELECT 
      Item,
      SUM(CASE WHEN Cat1 = 'Red' THEN 1 ELSE 0 END) AS Red,
      SUM(CASE WHEN Cat2 = 'New' THEN 1 ELSE 0 END) AS New
    FROM Table
    GROUP BY Item
    

    The idea here is that for all the Red rows, you assign a 1 (others get a 0) and you add up those 1’s and 0’s to get the count. Same thing for the New values.

    You can do the same thing with more brevity by exploiting MySQL’s 0/1 boolean evaluation too:

    SELECT 
      Item,
      SUM(Cat1 = 'Red') AS Red,
      SUM(Cat2 = 'New') AS New
    FROM Table
    GROUP BY Item
    

    In this example, Cat1 = 'Red' will return a 1 if true, and those get summed up. The SUM(CASE) method is going to be more portable across RDBMS other than MySQL though if other systems treat their booleans differently.

    Edit:

    Just to clarify then, in PHP you will retrieve these as $row['Red'] and $row['New']. You can just change the aliases to whatever you need: AS Red to AS CountItem to match your original…

    while($row = mysql_fetch_array($query) AND $row2 = mysql_fetch_array($query2)) {
      echo $row['Red'] . " " . $row2['New'] . " " . $row['Item'];
      echo "<br>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used this statements to populate jcombobox from MySql database table using Hibernate
I have my tags desinged like this in my database: Table: Item Columns: ItemID,
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database application in which a group is modeled like this: TABLE
I have a database table using an enum. This is already working with hibernate
I have written a function to print database table to an array like this
I always forget how to do things like this. I have a database table
I have a table like this in my database (SQL Server 2008) ID Type
I have a table in my postgresql 8.4 database like this: id(serial), event_type_id(id, foreign
I have created a table on an Oracle 10g database with this structure :

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.