我正在使用 CodeIgniter。我有一个员工表,记录是
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
现在我的问题在created_by
列中。当我显示任何 id 值的记录时,我会得到类似
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | 2
但是我的预期输出
id |firstname | lastname | mobileno | created_by
3 |jhg | uhgf | 1452365478 | mnb nbgfv
我必须显示created_by
的名称
我只尝试了这个查询。
$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;
}
最佳答案
我有一个提示(也许这可以解决您的问题)。 尝试这样的查询:
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'
通过上述查询,您无需创建临时表或其他附加表。 我测试过。 >>> http://sqlfiddle.com/#!9/e693cf/2/0
谢谢
关于php - 如果记录在一张表中,如何显示名称而不是 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53240694/