如何连接表以获取另一个表中一行的两个文件的描述

时间:2017-07-18 12:44:49

标签: codeigniter

我的表格有用户ID

table investigate
-----------------------------------
supervisor   reporter    score
-----------------------------------
 1                2         300
-----------------------------------

user table
----------------------
id             name
----------------------
1                vijay
2                 PHP
----------------------

here i need to join tables and get result as
----------------------------------
supervisor   reporter      score
--------------------------------
vijay         php          300
----------------------------------

在mysql中如何从表中获取相同条件下同一行的两条记录的描述结果

2 个答案:

答案 0 :(得分:1)

你可以在Codeigniter中尝试这样的事情。无法测试,但我希望它会有所帮助。

$this->db->select('user.name as user1, user.name as user2, investigate.score')
$this->db->from('investigate')
$this->db->join('users', 'investigate.supervisor = user1.id AND investigate.reporter = user2.id', 'left')
return $this->db->get()->result();

答案 1 :(得分:0)

在模型中尝试以下SQL查询

$sql = "SELECT (SELECT name FROM user as t2 WHERE t2.id = t1.supervisor) as supervisor, (SELECT name FROM user as t2 WHERE t2.id = t1.visitor) as visitor, score FROM investigate as t1";

$this->db->query($sql)->result();

希望这对你有用......