【发布时间】:2019-02-28 17:01:22
【问题描述】:
我正在尝试从同一天收集一堆值。这工作得很好,但我的问题是我想找到我在 SUM 中收集的值的 id。
我需要ID,因为我要提取一些数据到具有相同日期的ID。
这是我的表格的一个例子:
╔════╦══════════╦═════════╦═════════╦═════════════╗
║ id ║ q_tap_1 ║ q_tap_2 ║ q_tap_3 ║ datex ║
╠════╬══════════╬═════════╬═════════╬═════════════╣
║ 1 ║ -1.02 ║ 0.00 ║ 0.00 ║ 24.02.2019 ║
║ 2 ║ -23 ║ -1.23 ║ -354 ║ 24.02.2019 ║
║ 8 ║ -354 ║ -10.23 ║ -29 ║ 28.02.2019 ║
║ 4 ║ 0.00 ║ -4.23 ║ -1.53 ║ 23.02.2019 ║
║ 7 ║ -34 ║ 0.00 ║ 0.00 ║ 23.02.2019 ║
║ 6 ║ -100 ║ -13.63 ║ -54.23 ║ 28.02.2019 ║
║ 9 ║ -1.32 ║ 0.00 ║ 0.00 ║ 28.02.2019 ║
║ 10 ║ -23 ║ 0.00 ║ 0.00 ║ 21.02.2019 ║
║ 11 ║ -5.23 ║ -40.53 ║ -8.32 ║ 21.02.2019 ║
║ 12 ║ -23 ║ -1.23 ║ -23.75 ║ 28.02.2019 ║
╚════╩══════════╩═════════╩═════════╩═════════════╝
这就是它现在正在做的事情:
-349 (SUM of all the rows with the same date)
28.02.2019 (Date)
8 (Trying to get all the ids from the same day, but now its only showing the first)
-123
24.02.2019
1
-121
23.02.2019
4
-123
21.02.2019
10
这就是我想要的
-349
28.02.2019
8 6 9 12
-123
24.02.2019
1 2
-121
23.02.2019
4 7
-123
21.02.2019
10 11
$sql7 = 'SELECT DATE(datex) as datex, (SUM(q_tap_1) + SUM(q_tap_2) + SUM(q_tap_3) + SUM(q_tap_4) + SUM(q_tap_5) + SUM(q_tap_6)) AS tap, SUM(bonus_gevinst_1) AS gevinst, id FROM `test3` WHERE aktiv LIKE "Nei" GROUP BY DATE(datex) DESC';
$test7 = mysqli_query($conn,$sql7);
<?php
while ($test8 = mysqli_fetch_assoc($test7)) {
$test32 = $test8['gevinst'] + $test8['tap'];
echo '<form method="post">
<input type="hidden" name="id" value="'.$test8['datex'].'">
<li class="list-group-item text-secondary" style="border-color:#17a2b8;margin-bottom:5px;"><div style="display:-webkit-inline-box;"><p class="text-success" style="display: -webkit-inline-box;">'.$test32.'kr</p><br>
<p class="text-info" style="display: -webkit-inline-box;">'.$test8['datex'].'</p>
<p class="text-info" style="display: -webkit-inline-box;">'.$test8['id'].'</p>
</li>
</form>';
};
?>
【问题讨论】:
-
SUM 是一个聚合函数。所以它会加起来很多行,所以显示所有行的
id的概念是没有意义的,除非你在单独的查询中使用它 -
@RiggsFolly,我只是将值与 SUM 相加,但仍然想知道 id 是什么,以便我可以拥有 id 并在正确的日期下显示更多信息,因为我的表很多比这些显示的更大。
-
或者,如果您想查看所有行,则必须将它们全部选中并在 PHP 代码中处理 SUMmation