gpt4 book ai didi

mysql - Ruby Rails,在索引erb页面中获取db中列的重复计数

转载 作者:行者123 更新时间:2023-11-29 00:10:01 24 4
gpt4 key购买 nike

我在 ruby​​ rails 中有一些应用。我是这门语言的新手,我想在我的数据库 (mysql) 中找到我表的特定列中的重复记录。

      <th>Count Activation</th>
<td align="center" width="4%"><%= Database.select(:license_code).count %></td>

我输入它,这会得到 license_code 列并计算在我的应用程序中许可证代码的记录出现了多少次。

我需要找到 license_code 的重复值,以及找到的每个值的重复计数并将其放入我的 erb 页面(只是一个显示数据库的页面:

<table width="120%" align="center" border="2" class="display" id="mytable" style="margin:3px">

<thead>
<tr>
<th>Negozio</th>
<th>Cliente</th>
<th>Azienda</th>
<th>Referente</th>
<th>Cellulare</th>
<th>Email</th>
<th>Antivirus</th>
<th>Attivazione Effettiva</th>
<th>Attivazione al cliente</th>
<th>Scadenza Reale</th>
<th>License code</th>
<th>Licenza valida per n. pc</th>
<th>Licenza attivata su n. pc</th>
<th>Prezzo</th>
<th>Opzioni</th>
<th>Count Attivazioni</th>
</tr>
</thead>

<tbody>
<% @databases.each do |database| %>
<tr>
<td align="center" width="15%"><%= database.shop %></td>
<td align="center" width="5%"><%= database.customer %></td>
<td align="center" width="6%"><%= database.company %></td>
<td align="center" width="7%"><%= database.referer %></td>
<td align="center" width="10%"><%= database.phone %></td>
<td align="center" width="10%"><%= database.email %></td>
<td align="center" width="4%"><%= database.antivirus %></td>
<td align="center" width="7%"><%= database.activated_at %></td>
<td align="center" width="7%"><%= database.customer_activated_at %></td>
<td align="center" width="6%"><%= database.license_expires_at %></td>
<td align="center" width="16%"><%= database.license_code %></td>
<td align="center" width="4%"><%= database.license_activable_for_pc %></td>
<td align="center" width="4%"><%= database.license_activated_in %></td>
<td align="center" width="4%"><%= database.price %></td>
<td align="center" width="10%"><%= link_to 'Show', database %> | <%= link_to 'Edit', edit_database_path(database) %> |
<%= link_to 'Destroy', database, method: :delete, data: { confirm: 'Are you sure?' } %></td>

<td align="center" width="4%"><%= Database.select(:license_code).count %></td>
</tr>
<% end %>

有人可以帮助我吗? :)型

最佳答案

license_count 的重复值

class Database < ActiveRecord::Base
scope :duplicate_databases, -> {select(:license_code).group(:license_code).having("count(*) > 1")}
end

在 Controller 中

def action
@duplicate_records = Database.duplicate_databases
end

在 View 中

 <td> count activation:  <%= @duplicate_records.count %>  </td>

关于mysql - Ruby Rails,在索引erb页面中获取db中列的重复计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25620867/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com