How to manipulate SQL Query output












0















I would like to know if it's possible for example I have one sql table with the following results



+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+


And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?



Well with the answers bellow I managed to get it changed but now how can I echo the value in php?



SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table


Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.










share|improve this question




















  • 2





    select case Active when 1 then 'Yes' else 'No'

    – dnoeth
    Nov 20 '18 at 17:21
















0















I would like to know if it's possible for example I have one sql table with the following results



+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+


And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?



Well with the answers bellow I managed to get it changed but now how can I echo the value in php?



SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table


Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.










share|improve this question




















  • 2





    select case Active when 1 then 'Yes' else 'No'

    – dnoeth
    Nov 20 '18 at 17:21














0












0








0








I would like to know if it's possible for example I have one sql table with the following results



+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+


And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?



Well with the answers bellow I managed to get it changed but now how can I echo the value in php?



SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table


Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.










share|improve this question
















I would like to know if it's possible for example I have one sql table with the following results



+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+


And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?



Well with the answers bellow I managed to get it changed but now how can I echo the value in php?



SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table


Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 17:50







Saleen.S

















asked Nov 20 '18 at 17:17









Saleen.SSaleen.S

64




64








  • 2





    select case Active when 1 then 'Yes' else 'No'

    – dnoeth
    Nov 20 '18 at 17:21














  • 2





    select case Active when 1 then 'Yes' else 'No'

    – dnoeth
    Nov 20 '18 at 17:21








2




2





select case Active when 1 then 'Yes' else 'No'

– dnoeth
Nov 20 '18 at 17:21





select case Active when 1 then 'Yes' else 'No'

– dnoeth
Nov 20 '18 at 17:21












3 Answers
3






active

oldest

votes


















1














Just use a case statement for translating 1 = yes and 0 = No like this



select ID 
,Name
,Number
,case when Active=0 then 'No'
when Active=1 then 'Yes'
end as active_y_n
from table





share|improve this answer
























  • For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

    – Saleen.S
    Nov 20 '18 at 17:31











  • Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

    – Saleen.S
    Nov 20 '18 at 17:33



















0














use case when



select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t





share|improve this answer































    0














    A particularly simple way would use elt():



    select Id, name, number,
    elt(Active + 1, 'No', 'Yes') as as active_status
    from t





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398216%2fhow-to-manipulate-sql-query-output%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Just use a case statement for translating 1 = yes and 0 = No like this



      select ID 
      ,Name
      ,Number
      ,case when Active=0 then 'No'
      when Active=1 then 'Yes'
      end as active_y_n
      from table





      share|improve this answer
























      • For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

        – Saleen.S
        Nov 20 '18 at 17:31











      • Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

        – Saleen.S
        Nov 20 '18 at 17:33
















      1














      Just use a case statement for translating 1 = yes and 0 = No like this



      select ID 
      ,Name
      ,Number
      ,case when Active=0 then 'No'
      when Active=1 then 'Yes'
      end as active_y_n
      from table





      share|improve this answer
























      • For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

        – Saleen.S
        Nov 20 '18 at 17:31











      • Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

        – Saleen.S
        Nov 20 '18 at 17:33














      1












      1








      1







      Just use a case statement for translating 1 = yes and 0 = No like this



      select ID 
      ,Name
      ,Number
      ,case when Active=0 then 'No'
      when Active=1 then 'Yes'
      end as active_y_n
      from table





      share|improve this answer













      Just use a case statement for translating 1 = yes and 0 = No like this



      select ID 
      ,Name
      ,Number
      ,case when Active=0 then 'No'
      when Active=1 then 'Yes'
      end as active_y_n
      from table






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 '18 at 17:21









      George JosephGeorge Joseph

      1,44249




      1,44249













      • For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

        – Saleen.S
        Nov 20 '18 at 17:31











      • Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

        – Saleen.S
        Nov 20 '18 at 17:33



















      • For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

        – Saleen.S
        Nov 20 '18 at 17:31











      • Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

        – Saleen.S
        Nov 20 '18 at 17:33

















      For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

      – Saleen.S
      Nov 20 '18 at 17:31





      For example if I would like to echo the Yes or no in PHP I would have to use the var active_y_n?

      – Saleen.S
      Nov 20 '18 at 17:31













      Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

      – Saleen.S
      Nov 20 '18 at 17:33





      Would it be something like: $isActive = $rows['active_y_n']; and then echo $isActive;

      – Saleen.S
      Nov 20 '18 at 17:33













      0














      use case when



      select Id,name,number,
      case Active when 0 then 'No'
      when 1 then 'Yes' end as active_status
      from t





      share|improve this answer




























        0














        use case when



        select Id,name,number,
        case Active when 0 then 'No'
        when 1 then 'Yes' end as active_status
        from t





        share|improve this answer


























          0












          0








          0







          use case when



          select Id,name,number,
          case Active when 0 then 'No'
          when 1 then 'Yes' end as active_status
          from t





          share|improve this answer













          use case when



          select Id,name,number,
          case Active when 0 then 'No'
          when 1 then 'Yes' end as active_status
          from t






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 17:21









          Zaynul Abadin TuhinZaynul Abadin Tuhin

          12.4k2931




          12.4k2931























              0














              A particularly simple way would use elt():



              select Id, name, number,
              elt(Active + 1, 'No', 'Yes') as as active_status
              from t





              share|improve this answer




























                0














                A particularly simple way would use elt():



                select Id, name, number,
                elt(Active + 1, 'No', 'Yes') as as active_status
                from t





                share|improve this answer


























                  0












                  0








                  0







                  A particularly simple way would use elt():



                  select Id, name, number,
                  elt(Active + 1, 'No', 'Yes') as as active_status
                  from t





                  share|improve this answer













                  A particularly simple way would use elt():



                  select Id, name, number,
                  elt(Active + 1, 'No', 'Yes') as as active_status
                  from t






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 18:13









                  Gordon LinoffGordon Linoff

                  767k35300402




                  767k35300402






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398216%2fhow-to-manipulate-sql-query-output%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith