我有查询mysql的问题... 查询乘以金额,不应
表“订单”:
$locales = $request->input('locale');
foreach ($locales as $id => $attr)
{
$locale = Locale::find($id);
$locale->title = $attr['title'];
// ...
$locale->save();
}
表“付款”:
[ID, NAME ]
[22 , "Example"]
表“文件”:
[ID, ORDER_ID, AMOUNT]
[1 , 22 , 120 ]
我想得到:
[ID, ORDER_ID, NAME]
[1 , 22 , "Test 1"]
[2 , 22 , "Test 2"]
现在我收到了它(PRICE_SUM中的错误)
[ID, NAME , PRICE_SUM, INDEXS ]
[22, "Example", 120 , "Test 1, Test 2"]
我的查询:
[ID, NAME , PRICE_SUM, INDEXS ]
[22, "Example", 240 , "Test 1, Test 2"]
答案 0 :(得分:1)
我认为你需要一个相关的子查询,如下所示:
[http://plnkr.co/edit/mTzAIiMmiVzQfSkHGgoU?p=preview]
鉴于
SELECT
o.id,
o.name,
sum(p.amount) as price_sum,
(select GROUP_CONCAT(
d.name
)
from d
where d.order_id = o.id
) AS indexs
FROM o
LEFT JOIN p ON p.`order_id` = o.`id`
GROUP BY
o.`id`,o.name
ORDER BY
o.`id` DESC
查询结果
drop table if exists o,p,d;
create table o (ID int, NAME varchar(20));
insert into o values
(22 , 'Example');
create table p (ID INT, ORDER_ID INT, AMOUNT INT);
INSERT INTO P VALUES
(1 , 22 , 120 );
CREATE TABLE D (ID INT, ORDER_ID INT, NAME VARCHAR(20));
INSERT INTO D VALUES
(1 , 22 , 'Test 1'),
(2 , 22 , 'Test 2');
答案 1 :(得分:0)
在Documents
表中,您有两次Order_Id
22次 - 当Documents
与Payments
加入时,这个金额会加倍。