gpt4 book ai didi

php - 使用 join codeigniter 显示不在其他表中的数据

转载 作者:行者123 更新时间:2023-11-29 10:28:33 24 4
gpt4 key购买 nike

我有2个表,我尝试用codeigniter连接来合并2个表,但是当我创建查询连接时,有数据没有出现,因为数据不再在另一个表中,我该如何解决这个问题??

tbl_fruit
_______________
| id | fruit |
|----|--------|
| 0 | manggo |
| 1 | apple |
| 2 | banana |
| 3 | orange |
| 4 | avocado|

tbl_proc
_______________
| id | proc |
|----|--------|
| 0 | juice |
| 1 | blend |
| 2 | shake |

tbl_master
____________________________________
| id | id_fruit | id_proc | client |
|----|----------|---------|---------|
| 0 | 2 | | | //How display on join
| 1 | 1 | 2 | nana |
| 2 | 3 | 2 | george |
| 3 | 4 | 0 | smith |
| 4 | 0 | | billy | //How display on join
| 5 | 2 | 1 | Jack |

tbl_result
----------------------------------------
| no | name fruit | name proc | client |
|----|------------|-----------|--------|
| 0 | apple | shake | nana |
| 1 | orange | shake | george |
| 2 | avocado | juice | smith |
| 3 | banana | blend | smith |

$query = $this->db->select('*')
->from('tb_result')
->join('tb_fruit', 'tb_fruit.id = tb_result.id_fruit')
->join('tb_proc', 'tb_proc.id = tb_result.id_proc')
->get();
return $query->result();

如何显示tbl_master上其他表没有数据但出现在join的表中的数据?

最佳答案

在连接函数的第三个参数处使用 LEFT JOIN

$query = $this->db->select('*')
->from('tbl_master')
->join('tbl_fruit', 'tbl_fruit.id = tbl_master.id_fruit', 'left')
->join('tbl_proc', 'tbl_proc.id = tbl_master.id_proc', 'left')
->get();
return $query->result();

结果应该是 SQLFIDDLE

关于php - 使用 join codeigniter 显示不在其他表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47861126/

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