Replacing an output from an SQL statement with a different string









up vote
-1
down vote

favorite












I currently have an output that is populating a table. One of the outputs it gives is a year from the student's table. Whenever 6 is outputted as the year I need it to return L6 and when 7 is outputted I need U6. I have made an attempt bellow:



"$stmt = $conn->prepare(
"SELECT st.Name AS student, st.House AS house, T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
CASE
WHEN st.Year = 6 THEN 'L6'
WHEN st.Year = 7 THEN 'U6'
ELSE st.Year
END CASE as year
From Students AS st INNER JOIN Student_Choices AS sc
ON st.Username = sc.Username INNER JOIN Current_DB AS db
ON sc.DB_year = db.DB
INNER JOIN Choices AS c1
ON sc.T1_Choice = c1.Choice_ID
INNER JOIN Sports AS T1
ON c1.Sport_ID = T1.Sport_ID
INNER JOIN Choices AS c2
ON sc.T2_Choice = c2.Choice_ID
INNER JOIN Sports AS T2
ON c2.Sport_ID = T2.Sport_ID
INNER JOIN Choices AS c3
ON sc.T3_Choice = c3.Choice_ID
INNER JOIN Sports AS T3
ON c3.Sport_ID = T3.Sport_ID
");


I then get an error:




errorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN st.Year = 6 THEN 'L6' WHEN st.' at line 2




Any solutions would be great thanks.










share|improve this question



























    up vote
    -1
    down vote

    favorite












    I currently have an output that is populating a table. One of the outputs it gives is a year from the student's table. Whenever 6 is outputted as the year I need it to return L6 and when 7 is outputted I need U6. I have made an attempt bellow:



    "$stmt = $conn->prepare(
    "SELECT st.Name AS student, st.House AS house, T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
    CASE
    WHEN st.Year = 6 THEN 'L6'
    WHEN st.Year = 7 THEN 'U6'
    ELSE st.Year
    END CASE as year
    From Students AS st INNER JOIN Student_Choices AS sc
    ON st.Username = sc.Username INNER JOIN Current_DB AS db
    ON sc.DB_year = db.DB
    INNER JOIN Choices AS c1
    ON sc.T1_Choice = c1.Choice_ID
    INNER JOIN Sports AS T1
    ON c1.Sport_ID = T1.Sport_ID
    INNER JOIN Choices AS c2
    ON sc.T2_Choice = c2.Choice_ID
    INNER JOIN Sports AS T2
    ON c2.Sport_ID = T2.Sport_ID
    INNER JOIN Choices AS c3
    ON sc.T3_Choice = c3.Choice_ID
    INNER JOIN Sports AS T3
    ON c3.Sport_ID = T3.Sport_ID
    ");


    I then get an error:




    errorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN st.Year = 6 THEN 'L6' WHEN st.' at line 2




    Any solutions would be great thanks.










    share|improve this question

























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      I currently have an output that is populating a table. One of the outputs it gives is a year from the student's table. Whenever 6 is outputted as the year I need it to return L6 and when 7 is outputted I need U6. I have made an attempt bellow:



      "$stmt = $conn->prepare(
      "SELECT st.Name AS student, st.House AS house, T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
      CASE
      WHEN st.Year = 6 THEN 'L6'
      WHEN st.Year = 7 THEN 'U6'
      ELSE st.Year
      END CASE as year
      From Students AS st INNER JOIN Student_Choices AS sc
      ON st.Username = sc.Username INNER JOIN Current_DB AS db
      ON sc.DB_year = db.DB
      INNER JOIN Choices AS c1
      ON sc.T1_Choice = c1.Choice_ID
      INNER JOIN Sports AS T1
      ON c1.Sport_ID = T1.Sport_ID
      INNER JOIN Choices AS c2
      ON sc.T2_Choice = c2.Choice_ID
      INNER JOIN Sports AS T2
      ON c2.Sport_ID = T2.Sport_ID
      INNER JOIN Choices AS c3
      ON sc.T3_Choice = c3.Choice_ID
      INNER JOIN Sports AS T3
      ON c3.Sport_ID = T3.Sport_ID
      ");


      I then get an error:




      errorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN st.Year = 6 THEN 'L6' WHEN st.' at line 2




      Any solutions would be great thanks.










      share|improve this question















      I currently have an output that is populating a table. One of the outputs it gives is a year from the student's table. Whenever 6 is outputted as the year I need it to return L6 and when 7 is outputted I need U6. I have made an attempt bellow:



      "$stmt = $conn->prepare(
      "SELECT st.Name AS student, st.House AS house, T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
      CASE
      WHEN st.Year = 6 THEN 'L6'
      WHEN st.Year = 7 THEN 'U6'
      ELSE st.Year
      END CASE as year
      From Students AS st INNER JOIN Student_Choices AS sc
      ON st.Username = sc.Username INNER JOIN Current_DB AS db
      ON sc.DB_year = db.DB
      INNER JOIN Choices AS c1
      ON sc.T1_Choice = c1.Choice_ID
      INNER JOIN Sports AS T1
      ON c1.Sport_ID = T1.Sport_ID
      INNER JOIN Choices AS c2
      ON sc.T2_Choice = c2.Choice_ID
      INNER JOIN Sports AS T2
      ON c2.Sport_ID = T2.Sport_ID
      INNER JOIN Choices AS c3
      ON sc.T3_Choice = c3.Choice_ID
      INNER JOIN Sports AS T3
      ON c3.Sport_ID = T3.Sport_ID
      ");


      I then get an error:




      errorSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CASE WHEN st.Year = 6 THEN 'L6' WHEN st.' at line 2




      Any solutions would be great thanks.







      mysql sql mariadb case






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 13:13

























      asked Nov 10 at 12:15









      Toby Dixon Smith

      358




      358






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Answer is as follows:



          SELECT st.Name AS student, st.House AS house,
          (CASE WHEN st.Year = 6 THEN 'L6' WHEN st.Year = 7 THEN 'U6' ELSE st.Year END) as year,
          T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
          From Students AS st
          INNER JOIN Student_Choices AS sc
          ON st.Username = sc.Username INNER JOIN Current_DB AS db
          ON sc.DB_year = db.DB
          INNER JOIN Choices AS c1
          ON sc.T1_Choice = c1.Choice_ID
          INNER JOIN Sports AS T1
          ON c1.Sport_ID = T1.Sport_ID
          INNER JOIN Choices AS c2
          ON sc.T2_Choice = c2.Choice_ID
          INNER JOIN Sports AS T2
          ON c2.Sport_ID = T2.Sport_ID
          INNER JOIN Choices AS c3
          ON sc.T3_Choice = c3.Choice_ID
          INNER JOIN Sports AS T3
          ON c3.Sport_ID = T3.Sport_ID


          CASE needed to be in brackets and the as year needed to be after when being selected






          share|improve this answer




















          • Mark your own answer as accepted (green checkmark) for closure.
            – Madhur Bhaiya
            Nov 11 at 5:58

















          up vote
          0
          down vote













          The case statements cannot sit on their own like that, they have to be part of the main query. Remove them and instead try



          SELECT 
          st.Name AS student,
          st.House AS house,
          CASE
          WHEN st.Year = 6 THEN 'L6'
          WHEN st.Year = 7 THEN 'U6'
          ELSE st.Year
          END CASE as year


          ...etc






          share|improve this answer




















          • Still not working, I've updated the question @ADyson
            – Toby Dixon Smith
            Nov 10 at 13:14










          • you missed a comma after T3 I think. You need a comma between each field.
            – ADyson
            Nov 10 at 15:57










          • Nope it wasn't that @ADyson
            – Toby Dixon Smith
            yesterday











          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',
          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%2f53238853%2freplacing-an-output-from-an-sql-statement-with-a-different-string%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Answer is as follows:



          SELECT st.Name AS student, st.House AS house,
          (CASE WHEN st.Year = 6 THEN 'L6' WHEN st.Year = 7 THEN 'U6' ELSE st.Year END) as year,
          T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
          From Students AS st
          INNER JOIN Student_Choices AS sc
          ON st.Username = sc.Username INNER JOIN Current_DB AS db
          ON sc.DB_year = db.DB
          INNER JOIN Choices AS c1
          ON sc.T1_Choice = c1.Choice_ID
          INNER JOIN Sports AS T1
          ON c1.Sport_ID = T1.Sport_ID
          INNER JOIN Choices AS c2
          ON sc.T2_Choice = c2.Choice_ID
          INNER JOIN Sports AS T2
          ON c2.Sport_ID = T2.Sport_ID
          INNER JOIN Choices AS c3
          ON sc.T3_Choice = c3.Choice_ID
          INNER JOIN Sports AS T3
          ON c3.Sport_ID = T3.Sport_ID


          CASE needed to be in brackets and the as year needed to be after when being selected






          share|improve this answer




















          • Mark your own answer as accepted (green checkmark) for closure.
            – Madhur Bhaiya
            Nov 11 at 5:58














          up vote
          1
          down vote



          accepted










          Answer is as follows:



          SELECT st.Name AS student, st.House AS house,
          (CASE WHEN st.Year = 6 THEN 'L6' WHEN st.Year = 7 THEN 'U6' ELSE st.Year END) as year,
          T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
          From Students AS st
          INNER JOIN Student_Choices AS sc
          ON st.Username = sc.Username INNER JOIN Current_DB AS db
          ON sc.DB_year = db.DB
          INNER JOIN Choices AS c1
          ON sc.T1_Choice = c1.Choice_ID
          INNER JOIN Sports AS T1
          ON c1.Sport_ID = T1.Sport_ID
          INNER JOIN Choices AS c2
          ON sc.T2_Choice = c2.Choice_ID
          INNER JOIN Sports AS T2
          ON c2.Sport_ID = T2.Sport_ID
          INNER JOIN Choices AS c3
          ON sc.T3_Choice = c3.Choice_ID
          INNER JOIN Sports AS T3
          ON c3.Sport_ID = T3.Sport_ID


          CASE needed to be in brackets and the as year needed to be after when being selected






          share|improve this answer




















          • Mark your own answer as accepted (green checkmark) for closure.
            – Madhur Bhaiya
            Nov 11 at 5:58












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Answer is as follows:



          SELECT st.Name AS student, st.House AS house,
          (CASE WHEN st.Year = 6 THEN 'L6' WHEN st.Year = 7 THEN 'U6' ELSE st.Year END) as year,
          T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
          From Students AS st
          INNER JOIN Student_Choices AS sc
          ON st.Username = sc.Username INNER JOIN Current_DB AS db
          ON sc.DB_year = db.DB
          INNER JOIN Choices AS c1
          ON sc.T1_Choice = c1.Choice_ID
          INNER JOIN Sports AS T1
          ON c1.Sport_ID = T1.Sport_ID
          INNER JOIN Choices AS c2
          ON sc.T2_Choice = c2.Choice_ID
          INNER JOIN Sports AS T2
          ON c2.Sport_ID = T2.Sport_ID
          INNER JOIN Choices AS c3
          ON sc.T3_Choice = c3.Choice_ID
          INNER JOIN Sports AS T3
          ON c3.Sport_ID = T3.Sport_ID


          CASE needed to be in brackets and the as year needed to be after when being selected






          share|improve this answer












          Answer is as follows:



          SELECT st.Name AS student, st.House AS house,
          (CASE WHEN st.Year = 6 THEN 'L6' WHEN st.Year = 7 THEN 'U6' ELSE st.Year END) as year,
          T1.Name AS T1, T2.Name AS T2, T3.Name AS T3
          From Students AS st
          INNER JOIN Student_Choices AS sc
          ON st.Username = sc.Username INNER JOIN Current_DB AS db
          ON sc.DB_year = db.DB
          INNER JOIN Choices AS c1
          ON sc.T1_Choice = c1.Choice_ID
          INNER JOIN Sports AS T1
          ON c1.Sport_ID = T1.Sport_ID
          INNER JOIN Choices AS c2
          ON sc.T2_Choice = c2.Choice_ID
          INNER JOIN Sports AS T2
          ON c2.Sport_ID = T2.Sport_ID
          INNER JOIN Choices AS c3
          ON sc.T3_Choice = c3.Choice_ID
          INNER JOIN Sports AS T3
          ON c3.Sport_ID = T3.Sport_ID


          CASE needed to be in brackets and the as year needed to be after when being selected







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 16:14









          Toby Dixon Smith

          358




          358











          • Mark your own answer as accepted (green checkmark) for closure.
            – Madhur Bhaiya
            Nov 11 at 5:58
















          • Mark your own answer as accepted (green checkmark) for closure.
            – Madhur Bhaiya
            Nov 11 at 5:58















          Mark your own answer as accepted (green checkmark) for closure.
          – Madhur Bhaiya
          Nov 11 at 5:58




          Mark your own answer as accepted (green checkmark) for closure.
          – Madhur Bhaiya
          Nov 11 at 5:58












          up vote
          0
          down vote













          The case statements cannot sit on their own like that, they have to be part of the main query. Remove them and instead try



          SELECT 
          st.Name AS student,
          st.House AS house,
          CASE
          WHEN st.Year = 6 THEN 'L6'
          WHEN st.Year = 7 THEN 'U6'
          ELSE st.Year
          END CASE as year


          ...etc






          share|improve this answer




















          • Still not working, I've updated the question @ADyson
            – Toby Dixon Smith
            Nov 10 at 13:14










          • you missed a comma after T3 I think. You need a comma between each field.
            – ADyson
            Nov 10 at 15:57










          • Nope it wasn't that @ADyson
            – Toby Dixon Smith
            yesterday















          up vote
          0
          down vote













          The case statements cannot sit on their own like that, they have to be part of the main query. Remove them and instead try



          SELECT 
          st.Name AS student,
          st.House AS house,
          CASE
          WHEN st.Year = 6 THEN 'L6'
          WHEN st.Year = 7 THEN 'U6'
          ELSE st.Year
          END CASE as year


          ...etc






          share|improve this answer




















          • Still not working, I've updated the question @ADyson
            – Toby Dixon Smith
            Nov 10 at 13:14










          • you missed a comma after T3 I think. You need a comma between each field.
            – ADyson
            Nov 10 at 15:57










          • Nope it wasn't that @ADyson
            – Toby Dixon Smith
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          The case statements cannot sit on their own like that, they have to be part of the main query. Remove them and instead try



          SELECT 
          st.Name AS student,
          st.House AS house,
          CASE
          WHEN st.Year = 6 THEN 'L6'
          WHEN st.Year = 7 THEN 'U6'
          ELSE st.Year
          END CASE as year


          ...etc






          share|improve this answer












          The case statements cannot sit on their own like that, they have to be part of the main query. Remove them and instead try



          SELECT 
          st.Name AS student,
          st.House AS house,
          CASE
          WHEN st.Year = 6 THEN 'L6'
          WHEN st.Year = 7 THEN 'U6'
          ELSE st.Year
          END CASE as year


          ...etc







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 10 at 12:33









          ADyson

          21.5k112441




          21.5k112441











          • Still not working, I've updated the question @ADyson
            – Toby Dixon Smith
            Nov 10 at 13:14










          • you missed a comma after T3 I think. You need a comma between each field.
            – ADyson
            Nov 10 at 15:57










          • Nope it wasn't that @ADyson
            – Toby Dixon Smith
            yesterday

















          • Still not working, I've updated the question @ADyson
            – Toby Dixon Smith
            Nov 10 at 13:14










          • you missed a comma after T3 I think. You need a comma between each field.
            – ADyson
            Nov 10 at 15:57










          • Nope it wasn't that @ADyson
            – Toby Dixon Smith
            yesterday
















          Still not working, I've updated the question @ADyson
          – Toby Dixon Smith
          Nov 10 at 13:14




          Still not working, I've updated the question @ADyson
          – Toby Dixon Smith
          Nov 10 at 13:14












          you missed a comma after T3 I think. You need a comma between each field.
          – ADyson
          Nov 10 at 15:57




          you missed a comma after T3 I think. You need a comma between each field.
          – ADyson
          Nov 10 at 15:57












          Nope it wasn't that @ADyson
          – Toby Dixon Smith
          yesterday





          Nope it wasn't that @ADyson
          – Toby Dixon Smith
          yesterday


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238853%2freplacing-an-output-from-an-sql-statement-with-a-different-string%23new-answer', 'question_page');

          );

          Post as a guest














































































          Popular posts from this blog

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          Evgeni Malkin