[英]define value of column from table 1 as value in table 2
我有2張桌子
表1包含
id site
1 A1
2 A2
3 B1
4 B2
表2包含
nesite fesite
A1 A2
B1 A1
A2 B2
我可以在表1中回顯表2作為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();
}
您可以使用不同的列將table1與表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)
活動記錄查詢將類似於
$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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.