How to display name instead of id if records are in one table?











up vote
0
down vote

favorite












I am using CodeIgniter. I have an employee table and records are



id |firstname | lastname | mobileno   | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2


Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like



    id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | 2


But my expected output



id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv


I have to display the name of created_by



I tried only this query.



   $get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}









share|improve this question






















  • I'm assuming the created_by is the id of another user, so join to the same table using this column.
    – Nigel Ren
    Nov 10 at 16:05










  • @NigelRen, Yes, I have only one table and created_by is the id of the table.
    – user9437856
    Nov 10 at 16:06















up vote
0
down vote

favorite












I am using CodeIgniter. I have an employee table and records are



id |firstname | lastname | mobileno   | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2


Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like



    id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | 2


But my expected output



id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv


I have to display the name of created_by



I tried only this query.



   $get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}









share|improve this question






















  • I'm assuming the created_by is the id of another user, so join to the same table using this column.
    – Nigel Ren
    Nov 10 at 16:05










  • @NigelRen, Yes, I have only one table and created_by is the id of the table.
    – user9437856
    Nov 10 at 16:06













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am using CodeIgniter. I have an employee table and records are



id |firstname | lastname | mobileno   | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2


Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like



    id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | 2


But my expected output



id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv


I have to display the name of created_by



I tried only this query.



   $get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}









share|improve this question













I am using CodeIgniter. I have an employee table and records are



id |firstname | lastname | mobileno   | created_by
2 |mnb | nbgfv | 1452145625 | 1
3 |jhg | uhgf | 1452365478 | 2
4 |poi | ijuy | 1458745632 | 2
5 |tgf | tgfd | 1458745254 | 2
6 |wer | qwes | 1523654512 | 2


Now My issue is in the column created_by. When I am displaying the record of any id value then I am getting the output like



    id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | 2


But my expected output



id |firstname | lastname | mobileno   | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv


I have to display the name of created_by



I tried only this query.



   $get_single_emp_record = array('id' => 3);
$this->db->where($get_single_emp_record);
$query = $this->db->get('tbl_employee');
$result = $query->row();
if($result)
{
return $result;
}
else
{
return 0;
}






php mysql codeigniter-3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 15:56









user9437856

350111




350111












  • I'm assuming the created_by is the id of another user, so join to the same table using this column.
    – Nigel Ren
    Nov 10 at 16:05










  • @NigelRen, Yes, I have only one table and created_by is the id of the table.
    – user9437856
    Nov 10 at 16:06


















  • I'm assuming the created_by is the id of another user, so join to the same table using this column.
    – Nigel Ren
    Nov 10 at 16:05










  • @NigelRen, Yes, I have only one table and created_by is the id of the table.
    – user9437856
    Nov 10 at 16:06
















I'm assuming the created_by is the id of another user, so join to the same table using this column.
– Nigel Ren
Nov 10 at 16:05




I'm assuming the created_by is the id of another user, so join to the same table using this column.
– Nigel Ren
Nov 10 at 16:05












@NigelRen, Yes, I have only one table and created_by is the id of the table.
– user9437856
Nov 10 at 16:06




@NigelRen, Yes, I have only one table and created_by is the id of the table.
– user9437856
Nov 10 at 16:06












2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










I have a hint (maybe this can give solution in your problem).
Try query like this :



SELECT 
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'


With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0



Hopefully can help you. Thanks






share|improve this answer








New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Give me some time to check.I think your query is working.
    – user9437856
    Nov 10 at 17:04










  • Thanks, @cobaek, It's working perfectly
    – user9437856
    Nov 10 at 17:10










  • You're welcome. Glad to help you
    – cobaek
    Nov 10 at 18:11


















up vote
0
down vote













you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation






share|improve this answer





















  • Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
    – user9437856
    Nov 10 at 16:03












  • I believe thats the best way to go about it if you want to achieve your goal
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:06










  • Just two tables will do the magic employees table and creator table then perform a join
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:08











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%2f53240694%2fhow-to-display-name-instead-of-id-if-records-are-in-one-table%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










I have a hint (maybe this can give solution in your problem).
Try query like this :



SELECT 
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'


With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0



Hopefully can help you. Thanks






share|improve this answer








New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Give me some time to check.I think your query is working.
    – user9437856
    Nov 10 at 17:04










  • Thanks, @cobaek, It's working perfectly
    – user9437856
    Nov 10 at 17:10










  • You're welcome. Glad to help you
    – cobaek
    Nov 10 at 18:11















up vote
0
down vote



accepted










I have a hint (maybe this can give solution in your problem).
Try query like this :



SELECT 
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'


With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0



Hopefully can help you. Thanks






share|improve this answer








New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Give me some time to check.I think your query is working.
    – user9437856
    Nov 10 at 17:04










  • Thanks, @cobaek, It's working perfectly
    – user9437856
    Nov 10 at 17:10










  • You're welcome. Glad to help you
    – cobaek
    Nov 10 at 18:11













up vote
0
down vote



accepted







up vote
0
down vote



accepted






I have a hint (maybe this can give solution in your problem).
Try query like this :



SELECT 
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'


With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0



Hopefully can help you. Thanks






share|improve this answer








New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









I have a hint (maybe this can give solution in your problem).
Try query like this :



SELECT 
t1.id , t1.firstname , t1.lastname ,t1.mobileno,
CONCAT(t2.firstname ," ",t2.lastname ) AS createby
FROM tbl_employee AS t1
JOIN tbl_employee AS t2 ON t2.id = t1.created_by
WHERE t1.id = '3'


With above query you no need create temporary table or other additional tables.
I Tested it. >>>
http://sqlfiddle.com/#!9/e693cf/2/0



Hopefully can help you. Thanks







share|improve this answer








New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer






New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered Nov 10 at 16:44









cobaek

163




163




New contributor




cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






cobaek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Give me some time to check.I think your query is working.
    – user9437856
    Nov 10 at 17:04










  • Thanks, @cobaek, It's working perfectly
    – user9437856
    Nov 10 at 17:10










  • You're welcome. Glad to help you
    – cobaek
    Nov 10 at 18:11


















  • Give me some time to check.I think your query is working.
    – user9437856
    Nov 10 at 17:04










  • Thanks, @cobaek, It's working perfectly
    – user9437856
    Nov 10 at 17:10










  • You're welcome. Glad to help you
    – cobaek
    Nov 10 at 18:11
















Give me some time to check.I think your query is working.
– user9437856
Nov 10 at 17:04




Give me some time to check.I think your query is working.
– user9437856
Nov 10 at 17:04












Thanks, @cobaek, It's working perfectly
– user9437856
Nov 10 at 17:10




Thanks, @cobaek, It's working perfectly
– user9437856
Nov 10 at 17:10












You're welcome. Glad to help you
– cobaek
Nov 10 at 18:11




You're welcome. Glad to help you
– cobaek
Nov 10 at 18:11












up vote
0
down vote













you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation






share|improve this answer





















  • Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
    – user9437856
    Nov 10 at 16:03












  • I believe thats the best way to go about it if you want to achieve your goal
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:06










  • Just two tables will do the magic employees table and creator table then perform a join
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:08















up vote
0
down vote













you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation






share|improve this answer





















  • Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
    – user9437856
    Nov 10 at 16:03












  • I believe thats the best way to go about it if you want to achieve your goal
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:06










  • Just two tables will do the magic employees table and creator table then perform a join
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:08













up vote
0
down vote










up vote
0
down vote









you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation






share|improve this answer












you will have to create another table maybe tbl_creator which will have the id, creator_name then in your query you will perform a Join operation







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 16:02









Carsim Rheedwan Adedotun Holuw

435




435












  • Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
    – user9437856
    Nov 10 at 16:03












  • I believe thats the best way to go about it if you want to achieve your goal
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:06










  • Just two tables will do the magic employees table and creator table then perform a join
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:08


















  • Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
    – user9437856
    Nov 10 at 16:03












  • I believe thats the best way to go about it if you want to achieve your goal
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:06










  • Just two tables will do the magic employees table and creator table then perform a join
    – Carsim Rheedwan Adedotun Holuw
    Nov 10 at 16:08
















Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
– user9437856
Nov 10 at 16:03






Any other way to display? because I don't want to create one more table. I have already more than 200 records in the table.
– user9437856
Nov 10 at 16:03














I believe thats the best way to go about it if you want to achieve your goal
– Carsim Rheedwan Adedotun Holuw
Nov 10 at 16:06




I believe thats the best way to go about it if you want to achieve your goal
– Carsim Rheedwan Adedotun Holuw
Nov 10 at 16:06












Just two tables will do the magic employees table and creator table then perform a join
– Carsim Rheedwan Adedotun Holuw
Nov 10 at 16:08




Just two tables will do the magic employees table and creator table then perform a join
– Carsim Rheedwan Adedotun Holuw
Nov 10 at 16:08


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240694%2fhow-to-display-name-instead-of-id-if-records-are-in-one-table%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

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma