我正在Codeigniter中创建Restful API,并且有2个表保存表orders
中的数据。表Orders
具有基本信息列id
,customer
,status
和observation
。并且Orders_products
返回有关订单的产品信息,该表具有名为orders_id
(具有关系),size
,qty
,price
和subtotal
的列。
是否有可能在一个请购单中从2个表中调用此值?
我期望的示例:Inside 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/