gpt4 book ai didi

php - 将表 1 中的列值定义为表 2 中的值

转载 作者:行者123 更新时间:2023-11-29 06:54:05 25 4
gpt4 key购买 nike

我有 2 张 table

表1包含

 id             site
1 A1
2 A2
3 B1
4 B2

表2包含

 nesite         fesite     
A1 A2
B1 A1
A2 B2

我可以将表 2 作为表 1 中的 ID 进行回显吗?例如,

nesite         fesite     
1 2
3 1
2 4

到目前为止,我已经尝试了一些查询,但没有任何效果。这是我模型的最后一个形状。

function site(){
$this->db->select('*');
$this->db->from('table2');
$this->db->join('table1','table1.site = table2.nesite OR table1.site = table2.fesite');
$query = $this->db->get();
return $query->result();
}

最佳答案

您可以使用不同的列将表1与表b连接两次,例如

select a1.id as nesite,
a2.id as fesite
from table2 b
join table1 a1 on (b.nesite = a1.site)
join table1 a2 on (b.fesite = a2.site)

DEMO

事件记录查询类似于

$this->db->select('a1.id as nesite,a2.id as fesite',FALSE);
$this->db->from('table2 b');
$this->db->join('table1 a1','b.nesite = a1.site');
$this->db->join('table1 a2','b.fesite = a2.site');
$query = $this->db->get();
$query->result();

关于php - 将表 1 中的列值定义为表 2 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46373608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com