[英]How to left join 2 tables with SUM() and MAX() grouped by date
i have table loadhistory (planning to "select only highest value per date" group by and order by date DESC) 我有表loadhistory(计划“按日期选择最高值”组和按日期DESC排序)
| user_id | customer_id | date | bal |
1 1 2015-02-27 500
2 1 2015-02-27 650
3 1 2015-02-28 450
4 1 2015-02-28 620
and table transactionrecord (and planning to sum up values per date using SUM(bal) group by and order by date DESC) 和表事务记录(并计划使用SUM(bal)组和按日期DESC排序每个日期的值)
| user_id | customer_id | date | bal |
1 1 2015-02-27 50
2 1 2015-02-27 20
3 1 2015-02-28 10
But i want to join the 2 tables which would look like this: 但我想加入2个表,看起来像这样:
| date | balance | amount paid |
2015-02-28 620 10
2015-02-27 650 70
im not good in joining tables. 我不擅长加入表格。 this is my code so far and not working
到目前为止这是我的代码,不起作用
$q = "SELECT a.customer_id, SUM(a.bal), a.date, MAX(b.bal) GROUP BY date
FROM transactionrecord as a
LEFT JOIN loadhistory as b ON b.customer_id = a.customer_id
WHERE customer_id = {$_COOKIE['id']} GROUP BY date
ORDER BY date DESC";
$r = @mysqli_query ($dbc, $q );
echo '<table align="center" cellspacing="0" cellpadding="5" width="45%">
<tr>
<td align="center"><b>Date</b></td>
<td align="center"><b>Balance</b></td>
<td align="center"><b>>Amount Paid></b></td>
</tr>';
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '
<td align="center">' . $row['date'] . '</td>
<td align="center">' . $row['MAX(b.bal)'] . '</td>
<td align="center">' . $row['SUM(a.bal)'] . '</td>
';
what to change in my query to join the 2 tables with the SUM() and MAX() included? 在我的查询中要更改什么来加入包含SUM()和MAX()的2个表? And is my using of $row[' '] in echo right?
我在echo中使用$ row ['']是对吗?
thanks a lot. 非常感谢。
Your SQL statement should look like this in order to achieve your desired result: 您的SQL语句应该如下所示,以实现您想要的结果:
SELECT a.customer_id, a.date, MAX(COALESCE(b.bal, 0)) AS bal, a.paid
FROM (
SELECT customer_id, date, SUM(bal) AS paid
FROM transactionrecord
GROUP BY customer_id, date
) AS a LEFT JOIN loadhistory AS b
ON a.customer_id = b.customer_id AND a.date = b.date
WHERE a.customer_id = 1
GROUP BY a.customer_id, a.date, a.paid
ORDER BY a.date DESC
And in your php, you cannot refer to result columns using MAX(b.bal)
or SUM(a.bal)
; 在你的php中,你不能使用
MAX(b.bal)
或SUM(a.bal)
来引用结果列; instead, you must alias the columns as I've done above. 相反,你必须像我上面所做的那样对列进行别名。 So you can refer to
MAX(b.bal)
as bal
and you can refer to SUM(a.bal)
as paid
. 所以你可以将
MAX(b.bal)
称为bal
,你可以将SUM(a.bal)
称为paid
。
You mostly had the SQL right, I only 你大多数都拥有SQL权利,我只是
GROUP BY
expression, GROUP BY
表达式, SUM
and MAX
) columns, SUM
和MAX
)列添加了别名, loadhistory
table loadhistory
表中多行的乘法结果 date
and customer_id
columns, since those columns appear in both tables, date
和customer_id
列,因为这些列出现在两个表中, COALESCE
on bal
in case the left join results in no matching loadhistory
records, loadhistory
记录,则在bal
上添加COALESCE
, date
since records must have equivalent customer_id
and date
to match your requirements, and date
添加了一个连接标准,因为记录必须具有与您的要求相当的等效customer_id
和date
,以及 customer_id
to the GROUP BY
clause because any non-aggregated fields in the SELECT
clause should be in the GROUP BY
clause for predictable results. customer_id
添加到GROUP BY
子句中,因为SELECT
子句中的任何非聚合字段都应位于GROUP BY
子句中以获得可预测的结果。 If you wanted to select the value with highest user_id
per date
in the table loadhistory
instead of MAX(bal)
, you would need to do something like this: 如果你想在表
loadhistory
而不是MAX(bal)
选择每个date
具有最高user_id
的值,你需要做类似这样的事情:
SELECT b.user_id, a.customer_id, a.date,
COALESCE(b.bal, 0) AS bal, SUM(a.bal) AS paid
FROM transactionrecord AS a LEFT JOIN (
SELECT h1.user_id, h1.customer_id, h1.date, h1.bal
FROM loadhistory h1 INNER JOIN (
SELECT MAX(user_id) AS user_id, customer_id, date
FROM loadhistory GROUP BY customer_id, date
) AS h2 ON h1.user_id = h2.user_id
AND h1.customer_id = h2.customer_id
AND h1.date = h2.date
) AS b ON a.customer_id = b.customer_id AND a.date = b.date
WHERE a.customer_id = 1
GROUP BY b.user_id, a.customer_id, a.date, b.bal
ORDER BY a.date DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.