从具有相同ID的单个表中获取多个记录

发布于 2025-01-22 23:40:53 字数 1158 浏览 0 评论 0原文

我正在用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

め七分饶幸 2025-01-29 23:40:53

查询的这种行为是正确的,为了获得所需的东西,需要进行查询。
假设您的存款中的此字段表:ID,USER_ID,名称,金额以及表中显示的数据。

您需要通过金额user_id

示例查询:

$deposit = Deposits::select(
                       'id',
                       'user_id',
                       'name',
                       'sum(amount)',
                    )->whereBetween('amount', [100, 1000])
                    ->groupBy('user_id')->get();

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 by user_id

Example query:

$deposit = Deposits::select(
                       'id',
                       'user_id',
                       'name',
                       'sum(amount)',
                    )->whereBetween('amount', [100, 1000])
                    ->groupBy('user_id')->get();

假面具 2025-01-29 23:40:53
DB::table('deposits')
        ->selectRaw('user_id, SUM(amount) as total_amount')
        ->where('amount', '>', '100')
        ->where('amount', '<', '1000')
        ->groupBy('user_id')
        ->get();

结果是:

[
  {
    "user_id":9,
    "total_amount":850
  },
  {
    "user_id":12,
    "total_amount":800
  }
]
DB::table('deposits')
        ->selectRaw('user_id, SUM(amount) as total_amount')
        ->where('amount', '>', '100')
        ->where('amount', '<', '1000')
        ->groupBy('user_id')
        ->get();

Result is:

[
  {
    "user_id":9,
    "total_amount":850
  },
  {
    "user_id":12,
    "total_amount":800
  }
]
黎歌 2025-01-29 23:40:53
app('db')
   ->table('users')
   ->select('name')
   ->selectRaw("SUM(amount) as total_amounts")
   ->havingBetween('total_amounts', [ 100, 1000 ])
   ->groupBy('user_id')
   ->get();

您可以将RAW查询用于每个用户的总和,并将其用于数量之和之间。

app('db')
   ->table('users')
   ->select('name')
   ->selectRaw("SUM(amount) as total_amounts")
   ->havingBetween('total_amounts', [ 100, 1000 ])
   ->groupBy('user_id')
   ->get();

You can use the raw query for the sum per user and use the having between for the sum of amounts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文