如果记录在一个表中,如何显示名称而不是ID?

时间:2018-11-10 15:56:29

标签: php mysql codeigniter-3

我正在使用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;
   }

2 个答案:

答案 0 :(得分:0)

您将不得不创建另一个表,例如tbl_creator,该表将具有id, creator_name,然后在您的查询中执行联接操作

答案 1 :(得分: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

希望能为您提供帮助。谢谢