Jump to content

Make table row as column in mysql


Go to solution Solved by Psycho,

Recommended Posts

Hi I have a query that look like this
 

             
               SELECT Count(tbltesttransactions.Trans_ID) as Passed
              
              ,tbltesttransactions.Status_ID as status
              ,tbltesttransactions.projectid as project                    
               FROM tbltesttransactions
               WHERE  tbltesttransactions.Status_ID = '1'
                 
              
               UNION
               
               SELECT Count(tbltesttransactions.Trans_ID) as Failed
              
           
               ,tbltesttransactions.Status_ID as status
               ,tbltesttransactions.projectid as project                     
               FROM tbltesttransactions
               WHERE  tbltesttransactions.Status_ID = '2'
              

But its output is like below:

counter           status            project

5                       Passed          project1

2                       Failed            project1

1                       Passed          project2

3                       Failed            project2


I want my output like this:

Project             Passed             Failed

project1               5                        2

project2               1                        3

Please help me :(
Your help is much appreciated.
 

Edited by samuel_lopez
Link to comment
https://forums.phpfreaks.com/topic/299576-make-table-row-as-column-in-mysql/
Share on other sites

Hi Ch0cu3r. 
THis is my code to output. I used Php

while ($row = $res->fetch_assoc()):
?>
     <tr>
          <td><?php echo strtoupper($row['project']); ?></td>
          <td><?php echo strtoupper($row['status']); ?></td>
          <td><?php echo strtoupper($row['counter']); ?></td>         
     
     </tr>
<?php
endwhile;
?>
  • Solution

@SamuelLopez:

 

Are there more than just the two statuses? If not, you should change the field to a Boolean. E.g. name the field "passed" and use 1 (TRUE) to indicate passed and 0 (FALSE) to indicate not passed.

 

@Barand,

 

Correct me if I am wrong, but I don't think the IF() statements are needed - just the conditions. A condition will resolve to either TRUE (1) or FALSE (0). So, this should work as well:

 

 

SELECT projectid as project 
     , SUM(Status_ID=1) as passed
     , SUM(Status_ID=2) as failed
FROM tbltesttransactions
GROUP BY projectid
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.