我们如何用 GROUP BY 加入结果
How can we join the results with GROUP BY
这里我加入了三个表并希望按 method.when 分组得到结果 我这样写查询
$query=$this->db->query("SELECT p.id, p.customerid, T.u_id, A.u_id, T.agent_name,
P.date as d_date, T.total_profit, A.t_price, A.t_dc
FROM bills P
LEFT JOIN((SELECT *,
bill_details.id as b_id,
bill_details.user_id as u_id,
SUM(bill_details.profit) AS total_profit
FROM bill_details
WHERE DATE(bill_details.bill_date) BETWEEN '{$start_date}' AND
'{$end_date}'
AND bill_details.user_id = 406
GROUP BY Date(bill_details.bill_date), bill_details.user_id) T)
ON p.id = T.bill_id
LEFT JOIN((SELECT *,
assigned_result.assigned_result_id as a_id,
assigned_result.user_id as u_id,
SUM(assigned_result.total_price) AS t_price,
SUM(assigned_result.total_dc) AS t_dc
FROM assigned_result
WHERE DATE(assigned_result.date) BETWEEN '{$start_date}' AND
'{$end_date}'
AND assigned_result.user_id = 406
GROUP BY Date(assigned_result.date), assigned_result.user_id) A)
ON p.id = A.bill_no
WHERE T.u_id = 406
OR A.u_id = 406");
return $query->result();
如果正在做 group_by 这样的条件 GROUP BY T.u_id,A.u_id
我的输出看起来像这样
Name Date Purchase Winning Balance
2019-07-25 240 -240
Ned 2019-07-25 132.60 0.00 132
我想要这样的结果
Name Date Purchase Winning Balance
Ned 2019-07-25 132.60 240 132
使用额外的 max()
聚合:
SELECT max(Name) as Name, Date, max(Purchase) as Purchase,
max(Winning) as Winning, max(Balance) as Balance
FROM
(
<YourOriginalQuery>
) q
GROUP BY Date;
您没有使用 GROUP BY
显示查询。但是根据您的描述,您可以执行以下任一操作:
- 完全删除
GROUP BY
。聚合查询将 return 一行。
- 使用`GROUP BY COALESCE(T.u_id, A.u_id)
如果这些列在 SELECT
.
中,您可能还需要调整 SELECT
子句
这里我加入了三个表并希望按 method.when 分组得到结果 我这样写查询
$query=$this->db->query("SELECT p.id, p.customerid, T.u_id, A.u_id, T.agent_name,
P.date as d_date, T.total_profit, A.t_price, A.t_dc
FROM bills P
LEFT JOIN((SELECT *,
bill_details.id as b_id,
bill_details.user_id as u_id,
SUM(bill_details.profit) AS total_profit
FROM bill_details
WHERE DATE(bill_details.bill_date) BETWEEN '{$start_date}' AND
'{$end_date}'
AND bill_details.user_id = 406
GROUP BY Date(bill_details.bill_date), bill_details.user_id) T)
ON p.id = T.bill_id
LEFT JOIN((SELECT *,
assigned_result.assigned_result_id as a_id,
assigned_result.user_id as u_id,
SUM(assigned_result.total_price) AS t_price,
SUM(assigned_result.total_dc) AS t_dc
FROM assigned_result
WHERE DATE(assigned_result.date) BETWEEN '{$start_date}' AND
'{$end_date}'
AND assigned_result.user_id = 406
GROUP BY Date(assigned_result.date), assigned_result.user_id) A)
ON p.id = A.bill_no
WHERE T.u_id = 406
OR A.u_id = 406");
return $query->result();
如果正在做 group_by 这样的条件 GROUP BY T.u_id,A.u_id
我的输出看起来像这样
Name Date Purchase Winning Balance
2019-07-25 240 -240
Ned 2019-07-25 132.60 0.00 132
我想要这样的结果
Name Date Purchase Winning Balance
Ned 2019-07-25 132.60 240 132
使用额外的 max()
聚合:
SELECT max(Name) as Name, Date, max(Purchase) as Purchase,
max(Winning) as Winning, max(Balance) as Balance
FROM
(
<YourOriginalQuery>
) q
GROUP BY Date;
您没有使用 GROUP BY
显示查询。但是根据您的描述,您可以执行以下任一操作:
- 完全删除
GROUP BY
。聚合查询将 return 一行。 - 使用`GROUP BY COALESCE(T.u_id, A.u_id)
如果这些列在 SELECT
.
SELECT
子句