gpt4 book ai didi

mysql - 复杂的sql连接表

转载 作者:太空宇宙 更新时间:2023-11-03 11:53:11 25 4
gpt4 key购买 nike

我有4张 table
图表chart_detail客户产品
像这样

enter image description here enter image description here

enter image description here enter image description here

我想运行 sql 命令来列出 chart 表中的数据

加入 cart 表和 chart detailcustomer 表没有问题因为

chart.id = chart_detail.id_chart
chart.id_customer = customer.id

DB::table('chart')
->join('customer', 'chart.id_customer', '=', 'customer.id')
->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')->get();

但我有问题无法访问nama_produk
(在chart_detail表中只有id_produk)...所以我需要加入chart表和produk

DB::table('chart')
->join('customer', 'chart.id_customer', '=', 'customer.id')
->join('produk', 'produk.id', '=', 'chart_detail.id_produk')
->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')->get();

但是我得到这样的错误

Column not found: 1054 Unknown column 'chart_detail.id_produk' in 'on clause'

因为在chart 表中没有可用的id_produk
id_produkchart_detail 表中可用

不知道怎么解决

最佳答案

您的连接顺序不正确,因此出现了关于未知列的错误。试试下面的代码:

DB::table('chart')
->join('customer', 'chart.id_customer', '=', 'customer.id')
->join('chart_detail', 'chart.id', '=', 'chart_detail.id_chart')
->join('produk', 'produk.id', '=', 'chart_detail.id_produk')->get();

关于mysql - 复杂的sql连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34524276/

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