做查询操作时,获得到的total_count不是预期的结果

发布于 2022-09-07 00:07:04 字数 2917 浏览 19 评论 0

这个查询操作我使用了查询插件(rummage_ecto),
以下是命令行测试:

iex(4)rummage = %{
...(4)>       search: %{name:  %{search_type: :like, search_term: "c"}},
...(4)>       sort: %{field: :name, order: :asc},
...(4)>       paginate: %{page: 1}
...(4)>     }
%{paginate: %{page: 1},
search: %{name: %{search_term: "c", search_type: :like}},
sort: %{field: :name, order: :asc}}

iex(5)> {queryable, rummage} = ZergApi.Base.Exchange.rummage(rummage)
[debug] QUERY OK source="exchanges" db=0.1ms
SELECT count(DISTINCT e0.`id`) FROM `exchanges` AS e0 []
{#Ecto.Query<from e in subquery(from e in subquery(from e in ZergApi.Base.Exchange),
where: like(e.name, ^"%c%")),
order_by: [asc: e.name], limit: ^10, offset: ^0>,
%{paginate: %{max_page: 1, page: 1, per_page: 10, total_count: 10},
 search: %{name: %{assoc: [], search_expr: :where, search_term: "c",
     search_type: :like}},
 sort: %{assoc: [], field: :name, order: :asc}}}

这个表中有十个数据。在搜索操作后,将会有三条数据被返回,即total_count = 3,但是实际返回了十条,即total_count = 10,和下面的是实际的数据。

下面是返回的实际数据

iex(6)> exchanges = queryable |> ZergApi.Repo.all
[debug] QUERY OK db=0.4ms
SELECT s0.`id`, s0.`name`, s0.`cn_name`, s0.`home_page`, s0.`timezone`, s0.`inserted_at`, s0.`updated_at` FROM (SELECT s0.`id` AS `id`, s0.`name` AS `name`, s0.`cn_name` AS `cn_name`, s0.`home_page` AS `home_page`, s0.`timezone` AS `timezone`, s0.`inserted_at` AS `inserted_at`, s0.`updated_at` AS `updated_at` FROM (SELECT e0.`id` AS `id`, e0.`name` AS `name`, e0.`cn_name` AS `cn_name`, e0.`home_page` AS `home_page`, e0.`timezone` AS `timezone`, e0.`inserted_at` AS `inserted_at`, e0.`updated_at` AS `updated_at` FROM `exchanges` AS e0) AS s0 WHERE (s0.`name` LIKE ?)) AS s0 ORDER BY s0.`name` LIMIT ? OFFSET ? ["%c%", 10, 0]
[%ZergApi.Base.Exchange{__meta__: #Ecto.Schema.Metadata<:loaded, "exchanges">,
  cn_name: "", home_page: "", id: 5,
  inserted_at: ~N[2018-04-10 14:02:17.000000], name: "",
  products: #Ecto.Association.NotLoaded<association :products is not loaded>,
  timezone: "Asia/Shanghai", updated_at: ~N[2018-04-10 14:02:17.000000]},
 %ZergApi.Base.Exchange{__meta__: #Ecto.Schema.Metadata<:loaded, "exchanges">,
  cn_name: "", home_page: "", id: 6,
  inserted_at: ~N[2018-04-10 14:02:17.000000], name: "",
  products: #Ecto.Association.NotLoaded<association :products is not loaded>,
  timezone: "Asia/Shanghai", updated_at: ~N[2018-04-10 14:02:17.000000]},
 %ZergApi.Base.Exchange{__meta__: #Ecto.Schema.Metadata<:loaded, "exchanges">,
  cn_name: "", home_page: "", id: 4,
  inserted_at: ~N[2018-04-10 14:02:17.000000], name: "",
  products: #Ecto.Association.NotLoaded<association :products is not loaded>,
  timezone: "Asia/Shanghai", updated_at: ~N[2018-04-10 14:02:17.000000]}]

各位能够提供一些建议吗

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文