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;
}
php mysql codeigniter-3
add a comment |
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;
}
php mysql codeigniter-3
I'm assuming thecreated_by
is theid
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
add a comment |
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;
}
php mysql codeigniter-3
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
php mysql codeigniter-3
asked Nov 10 at 15:56
user9437856
350111
350111
I'm assuming thecreated_by
is theid
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
add a comment |
I'm assuming thecreated_by
is theid
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
add a comment |
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
New contributor
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
add a comment |
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
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
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
add a comment |
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
New contributor
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
New contributor
New contributor
answered Nov 10 at 16:44
cobaek
163
163
New contributor
New contributor
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
add a comment |
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
add a comment |
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
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
Required, but never shown
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
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
I'm assuming the
created_by
is theid
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