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.
mysql sql mariadb case
add a comment |
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.
mysql sql mariadb case
add a comment |
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.
mysql sql mariadb case
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
mysql sql mariadb case
edited Nov 10 at 13:13
asked Nov 10 at 12:15
Toby Dixon Smith
358
358
add a comment |
add a comment |
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
Mark your own answer as accepted (green checkmark) for closure.
– Madhur Bhaiya
Nov 11 at 5:58
add a comment |
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
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
add a comment |
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
Mark your own answer as accepted (green checkmark) for closure.
– Madhur Bhaiya
Nov 11 at 5:58
add a comment |
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
Mark your own answer as accepted (green checkmark) for closure.
– Madhur Bhaiya
Nov 11 at 5:58
add a comment |
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
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
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password