How to manipulate SQL Query output
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
add a comment |
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
2
select case Active when 1 then 'Yes' else 'No'
– dnoeth
Nov 20 '18 at 17:21
add a comment |
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
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
mysql sql
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
For example if I would like to echo the Yes or no in PHP I would have to use the varactive_y_n
?
– Saleen.S
Nov 20 '18 at 17:31
Would it be something like:$isActive = $rows['active_y_n'];
and thenecho $isActive;
– Saleen.S
Nov 20 '18 at 17:33
add a comment |
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
add a comment |
A particularly simple way would use elt()
:
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t
add a comment |
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
});
}
});
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
Required, but never shown
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
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
For example if I would like to echo the Yes or no in PHP I would have to use the varactive_y_n
?
– Saleen.S
Nov 20 '18 at 17:31
Would it be something like:$isActive = $rows['active_y_n'];
and thenecho $isActive;
– Saleen.S
Nov 20 '18 at 17:33
add a comment |
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
For example if I would like to echo the Yes or no in PHP I would have to use the varactive_y_n
?
– Saleen.S
Nov 20 '18 at 17:31
Would it be something like:$isActive = $rows['active_y_n'];
and thenecho $isActive;
– Saleen.S
Nov 20 '18 at 17:33
add a comment |
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
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
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 varactive_y_n
?
– Saleen.S
Nov 20 '18 at 17:31
Would it be something like:$isActive = $rows['active_y_n'];
and thenecho $isActive;
– Saleen.S
Nov 20 '18 at 17:33
add a comment |
For example if I would like to echo the Yes or no in PHP I would have to use the varactive_y_n
?
– Saleen.S
Nov 20 '18 at 17:31
Would it be something like:$isActive = $rows['active_y_n'];
and thenecho $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
add a comment |
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
add a comment |
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
add a comment |
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
answered Nov 20 '18 at 17:21
Zaynul Abadin TuhinZaynul Abadin Tuhin
12.4k2931
12.4k2931
add a comment |
add a comment |
A particularly simple way would use elt()
:
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t
add a comment |
A particularly simple way would use elt()
:
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t
add a comment |
A particularly simple way would use elt()
:
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t
A particularly simple way would use elt()
:
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t
answered Nov 20 '18 at 18:13
Gordon LinoffGordon Linoff
767k35300402
767k35300402
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
2
select case Active when 1 then 'Yes' else 'No'
– dnoeth
Nov 20 '18 at 17:21