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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:19:41+00:00 2026-06-14T14:19:41+00:00

I have a table that has a status column, the status is a number

  • 0

I have a table that has a status column, the status is a number between 1-8.

I can use the query

SELECT status, COUNT(id) AS total 
FROM cart_orders 
GROUP BY status;

To get a result that looks like:

status | total
1      | 10
2      | 15
3      | 8
4      | 51
5      | 65 
...

But that means every time I need the data I have to do:

$status = array(1 => null, 2 => null, 3 => null, 4 => null, 
                5 => null, 6 => null, 7 => null, 8 => null);

foreach($rows as $row){
    $status[$row['status']] = $row['total'];
}

Which, while not a lot of code I would like to avoid having to do that,

What I want is to modify the query so I end up with a single row that looks like:

1  | 2  | 3  | 4  | 5   
10 | 15 | 8  | 51 | 65 ...

so I can just go e.g. $row['4']

The only what I can think of to do this would be to have each column as a subquery, which I would rather use the code than have 8 sub-queries. The other option is to join the table on itself 7 times, but that is assumedly also not ideal?

How else could I do this?

  • 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-14T14:19:42+00:00Added an answer on June 14, 2026 at 2:19 pm

    What you are asking to do is called a “pivot query,” which MySQL unfortunately does not support natively. What you must do is specify each row individually, as in:

    SELECT
       SUM(IF (status = 1, total, 0)) AS '1'
       ...
    

    ..but this is very verbose — especially if you have a lot of columns. I think your PHP is a little more verbose than it needs to be too in that you don’t need to declare the array ahead of time.

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

Sidebar

Related Questions

I have a table that has a column called 'status'. This can be set
I'm using SQL Server 2008 and I have a table that has a 'Status'
I have an application that has a status table see Database best practices -
I have one table that has two fields - ID1 and ID2 ID1 can
I'm trying to query a table which has a column that contains straight xml
I have a photos table in the database that uses a status column to
Suppose we have a table Users that has only one column UserId which is
I have a dictionary table called car_status that has an 'id' column and a
I have two tables: servers and stats servers has a column called id that
I have a table that has account numbers in (account_num) and user profiles (profile_id).

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.