从具有相同ID的单个表中获取多个记录
我正在用Laravel 5.8编写一个功能,其中我想获得所选范围内的用户金额的总和,我正在使用两个输入字段,以查看它们获得价格范围“ T0”,并且从“如果用户使用”输入100和1000,它应该显示具有金额&GT的用户列表; 100和金额< 1000。问题在于表格中所示的相同用户的多个记录。 我想总结用户的金额,并显示用户的金额在范围
表名称=存款范围内
<table>
<thead>
<tr>
<th>id</th>
<th>user_id</th>
<th>name</th>
<th>amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>ali</td>
<td>100</td>
</tr>
<tr>
<td>2</td>
<td>12</td>
<td>ali</td>
<td>800</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
<td>ali</td>
<td>50</td>
</tr>
<tr>
<td>4</td>
<td>15</td>
<td>khan</td>
<td>1100</td>
</tr>
<tr>
<td>6</td>
<td>9</td>
<td>james</td>
<td>850</td>
</tr>
<tr>
<td>7</td>
<td>9</td>
<td>james</td>
<td>90</td>
</tr>
</tbody>
</table>
I am writing a function in laravel 5.8 in which I want to get sum of the user amount that lies within my selected range, I am using the two input fields in view which they get the price range "T0 and From" e.g if the user enters 100 and 1000, it should show the list of the users who has amount > 100 and amount < 1000. The problem is there are multiple records of the same users as shown below in the table.
I want to sum the amount of the users and show the user if their amount is within range
table name= deposits
<table>
<thead>
<tr>
<th>id</th>
<th>user_id</th>
<th>name</th>
<th>amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>12</td>
<td>ali</td>
<td>100</td>
</tr>
<tr>
<td>2</td>
<td>12</td>
<td>ali</td>
<td>800</td>
</tr>
<tr>
<td>3</td>
<td>12</td>
<td>ali</td>
<td>50</td>
</tr>
<tr>
<td>4</td>
<td>15</td>
<td>khan</td>
<td>1100</td>
</tr>
<tr>
<td>6</td>
<td>9</td>
<td>james</td>
<td>850</td>
</tr>
<tr>
<td>7</td>
<td>9</td>
<td>james</td>
<td>90</td>
</tr>
</tbody>
</table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查询的这种行为是正确的,为了获得所需的东西,需要进行查询。
假设您的
存款中的此字段
表:ID,USER_ID,名称,金额
以及表中显示的数据。您需要通过
金额
按user_id
示例查询:
This behavior of your query is correct, to get what you need it is necessary to work on the query.
Assuming this fields in your
deposits
table:id, user_id, name, amount
in according with data showing in the table.You need to sum the
amount
grouping byuser_id
Example query:
结果是:
Result is:
您可以将RAW查询用于每个用户的总和,并将其用于数量之和之间。
You can use the raw query for the sum per user and use the having between for the sum of amounts.