多个表的多列连接到同一个表

ilmyapht  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(335)

我有个问题。
我在mysql数据库中有3个表。
我想用这些规则连接我的表,多个表的,多列连接到同一个表的一列。
mysql示例如下。提前谢谢。

SELECT
    s.*,e.* 
    , m1.m_id AS 'mid'
    , m1.m_name AS 'mname' 
    , m2.m_id AS 'bid'
    , m2.m_name AS 'bname'
    , e1.e_id AS 'tid'
    , e1.e_name AS 'tname'
    , e2.e_id AS 'topid'
    , e2.e_name AS 'topname'
    FROM s s,e e
    INNER JOIN m m1
    ON s.s_m_id = m1.m_id
    INNER JOIN m m2
    ON s.s_b_id = m2.m_id
    INNER JOIN e e1
    ON s.s_t_id = e1.e_id
    INNER JOIN e e2
    ON s.s_t_t_id = e2.e_id
    WHERE s_id =1
wxclj1h5

wxclj1h51#

你的代码不起作用吗?尝试删除上的“from”

$this->db->from("FROM s");
6vl6ewon

6vl6ewon2#

$this->db->select("s.*,e.* 
    , m1.m_id AS 'mid'
    , m1.m_name AS 'mname' 
    , m2.m_id AS 'bid'
    , m2.m_name AS 'bname'
    , e1.e_id AS 'tid'
    , e1.e_name AS 'tname'
    , e2.e_id AS 'topid'
    , e2.e_name AS 'topname'");
$this->db->from("FROM s");
$this->db->join("m m1
     s.s_m_id = m1.m_id","inner");
$this->db->join("m m2
     s.s_b_id = m2.m_id","inner");
$this->db->join("e e1
    s.s_t_id = e1.e_id","inner");
$this->db->join(" e e2
    s.s_t_t_id = e2.e_id","inner");
$this->db->where('s_id','1');
$this->db->get();

相关问题