gpt4 book ai didi

mysql - HQL/MySQL 用于列出不同项和重复项

转载 作者:行者123 更新时间:2023-11-29 22:08:26 25 4
gpt4 key购买 nike

我有 20.000 多个对象的列表。这些对象有一个名为 title 的表的 fk。如果两个tipp链接到相同的标题,并且它们属于同一个包(tipp_pkg_fk,这是一个参数),则它们被认为是重复的。我需要所有对象的列表,并将重复项列在一起。例如:

tippA.title.name = "One"
tippB.title.name = "Two"
tippC.title.name = "Two"

理想情况下,从上面我会得到这样的列表结果:[[tippA],[tippB,tippC]]

我不知道如何做到这一点,我已经尝试过(首先在Mysql中,这样我可以测试它,然后将其更改为HQL):

select tipp.tipp_id, 1 as sortOrder
from (select distinct a.tipp_id as id
from title_instance_package_platform a, title_instance_package_platform b
where a.tipp_pkg_fk= 1 and b.tipp_pkg_fk = 1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates,
title_instance_package_platform tipp
where tipp.tipp_id != duplicates.id
union all
select duplicates.id, 2 as sortOrder
from (select distinct a.tipp_id as id
from title_instance_package_platform a , title_instance_package_platform b
where a.tipp_pkg_fk = 1 and b.tipp_pkg_fk=1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates
order by sortOrder, id;

这个执行了 330 秒,然后我在 MySQL Workbench 中收到消息fetching,此时计算机开始死机。所以我的想法是,首先我选择所有不重复的 ID,然后选择所有重复的 ID,然后将它们合并并排序,以便它们出现在一起。我正在寻找最有效的方法来执行此操作,因为我将在通宵工作期间多次执行此查询。

对于我的 TIPP 模型,以下是映射的一部分:

static mapping = {
pkg column:'tipp_pkg_fk', index: 'tipp_idx'
title column:'tipp_ti_fk', index: 'tipp_idx'
}


+-----------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------+--------------+------+-----+---------+----------------+
| tipp_id | bigint(20) | NO | PRI | NULL | auto_increment |
| tipp_version | bigint(20) | NO | | NULL | |
| tipp_pkg_fk | bigint(20) | NO | MUL | NULL | |
| tipp_plat_fk | bigint(20) | NO | MUL | NULL | |
| tipp_ti_fk | bigint(20) | NO | MUL | NULL | |
| date_created | datetime | NO | | NULL | |
| last_updated | datetime | NO | | NULL | |



+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| ti_id | bigint(20) | NO | PRI | NULL | auto_increment |
| ti_version | bigint(20) | NO | | NULL | |
| date_created | datetime | NO | | NULL | |
| ti_imp_id | varchar(255) | NO | MUL | NULL | |
| last_updated | datetime | NO | | NULL | |
| ti_title | varchar(1024) | YES | | NULL | |
| ti_key_title | varchar(1024) | YES | | NULL | |
| ti_norm_title | varchar(1024) | YES | | NULL | |
| sort_title | varchar(1024) | YES | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+

更新

经过一些更改后,它正在工作:

select tipp.tipp_id as id, 1 as sortOrder
from
title_instance_package_platform tipp
where tipp.tipp_id not in (select distinct a.tipp_id as id
from title_instance_package_platform a, title_instance_package_platform b
where a.tipp_pkg_fk= 1 and b.tipp_pkg_fk = 1 and a.tipp_ti_fk = b.tipp_ti_fk)
union all
select duplicates.id as id, 2 as sortOrder
from (select distinct a.tipp_id as id
from title_instance_package_platform a , title_instance_package_platform b
where a.tipp_pkg_fk = 1 and b.tipp_pkg_fk=1 and a.tipp_ti_fk = b.tipp_ti_fk) duplicates
order by sortOrder, id;

尽管如此,我仍然没有将重复项分组在一起,而是所有内容都以列表的形式出现,这意味着我仍然需要将它们分组。

最佳答案

你能从另一边做选择吗?选择所有标题和包并列出所有提示,仅当存在提示时(计数> 0)并将它们捆绑在一起以获得您显示的数组?

关于mysql - HQL/MySQL 用于列出不同项和重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31921596/

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