gpt4 book ai didi

php - 在 API Rest 中从数组内的 2 个表中选择数据

转载 作者:行者123 更新时间:2023-11-29 15:34:00 25 4
gpt4 key购买 nike

我正在 Codeigniter 中创建 Restful API,并且有 2 个表用于保存表 orders 中的数据。表 Orders 具有基本信息列 idcustomerstatusobservationOrders_products 返回订单中产品的信息,该表有一列名为 orders_id(有关系)、sizeqty价格小计

可以在一份请购单中从 2 个表中调用此值吗?

我期望的示例:内部 Items 数组是 Orders_products 表。

{
"customer_id": 14,
"status": "2",
"items": [
{
"size": "S",
"qty": 10,
"price": 10.50,
"subprice": 100.50, // qty * price

},
{
"size": "M",
"qty": 2,
"price": 10.50,
"subprice": 21.00, // qty * price

}]
"observation": nothing,
}

最佳答案

试试这个在循环中使用 json_decode(items_json)

$this->db->select("t1.id, t1.customer, t1.status,t1.observation,CONCAT('[',t2.json_data,']') items_json");
$this->db->from("Orders t1");
$this->db->join("(select GROUP_CONCAT(CONCAT('{\"size\":\"',size,'\",\"qty\":','\"',qty,'\",\"price\":','\"',price,'\",\"subprice\":','\"',subprice,'\"}')) AS json_data,order_id
FROM Orders_products group by order_id) t2","t1.id=t2.order_id","left");
$this->db->get()->result_array();

关于php - 在 API Rest 中从数组内的 2 个表中选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58436321/

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