Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何避免一对多关联中的重复记录而不使用distinct?_Mysql_Sql_Postgresql - Fatal编程技术网

Mysql 如何避免一对多关联中的重复记录而不使用distinct?

Mysql 如何避免一对多关联中的重复记录而不使用distinct?,mysql,sql,postgresql,Mysql,Sql,Postgresql,我有两个表issues和time\u条目,它们之间有one-to-many关联。我们可以为一个问题创建多个time\u条目 下面是一些示例数据 问题 id | subject | description ------------------------------ 1 | test | test 2 | test1 | test1 3 | test2 | test2 时间条目 id | issue_id | hours | spent_

我有两个表
issues
time\u条目
,它们之间有
one-to-many
关联。我们可以为一个
问题创建多个
time\u条目

下面是一些示例数据

问题

id  |  subject  |  description
------------------------------
1   |  test     |  test
2   |  test1    |  test1
3   |  test2    |  test2
时间条目

id  | issue_id  |  hours | spent_on   | created_on
---------------------------------------------------
1   |  1        |  2     | 2016-12-23 | 2016-12-23
2   |  1        |  2     | 2016-12-23 | 2016-12-23
3   |  2        |  3     | 2016-12-23 | 2016-12-23
4   |  2        |  5     | 2016-12-23 | 2016-12-23
5   |  4        |  4     | 2016-12-23 | 2016-12-23
现在,我想获取在特定日期之后花费时间的所有
问题

SELECT *
FROM "issues"
INNER JOIN "time_entries" ON "time_entries"."issue_id" = "issues"."id"
WHERE time_entries.created_on > '2016-12-22'
它将返回具有多个条目的
问题
的多条记录

id   | subject  | description
-----------------------------
1   |  test     |  test
1   |  test     |  test
2   |  test1    |  test1
2   |  test1    |  test1
3   |  test2    |  test2

如何在不使用
distinct
的情况下避免这些重复记录。由于应用程序中的技术原因,我无法使用
distinct


我们将非常感谢您的帮助

一个选项是使用
选择DISTINCT

SELECT * FROM "issues" 
INNER JOIN "time_entries" ON "time_entries"."issue_id" = "issues"."id"
WHERE time_entries.created_on > '2016-12-22' 
group by id
SELECT DISTINCT t1.id,
                t1.subject,
                t1.description
FROM issues t1
INNER JOIN time_entries t2
    ON t2.issue_id = t1.id
WHERE t2.created_on > '2016-12-22'
如果不能使用
DISTINCT
GROUP BY
,则另一个选项是使用子查询,该子查询汇总
时间\u条目
表中的问题,并确定哪些问题符合要求。大概是这样的:

SELECT t1.id,
       t1.subject,
       t1.description
FROM issues t1
INNER JOIN
(
    SELECT issue_id
    FROM time_entries
    GROUP BY issue_id
    HAVING SUM(CASE WHEN created_on > '2016-12-22' THEN 1 ELSE 0 END) > 0
) t2
    ON t2.issue_id = t1.id

一个选项是使用
选择DISTINCT

SELECT DISTINCT t1.id,
                t1.subject,
                t1.description
FROM issues t1
INNER JOIN time_entries t2
    ON t2.issue_id = t1.id
WHERE t2.created_on > '2016-12-22'
如果不能使用
DISTINCT
GROUP BY
,则另一个选项是使用子查询,该子查询汇总
时间\u条目
表中的问题,并确定哪些问题符合要求。大概是这样的:

SELECT t1.id,
       t1.subject,
       t1.description
FROM issues t1
INNER JOIN
(
    SELECT issue_id
    FROM time_entries
    GROUP BY issue_id
    HAVING SUM(CASE WHEN created_on > '2016-12-22' THEN 1 ELSE 0 END) > 0
) t2
    ON t2.issue_id = t1.id

您使用的是哪个数据库,MySQL还是Postgres?它们之间有很大的不同。你使用的是哪个数据库,MySQL还是Postgres?它们之间有很大的不同。由于我的应用程序中存在一些技术问题,我不能使用
distinct
group by
。@RamizRaja我给了你一个不需要这两个选项的选项。只是好奇,postgres是否有
exists
?@Alex是的,postgres有一个
exists
子句,q.v。这可能是另一种不使用
GROUP BY
DISTINCT
的方法。由于我的应用程序中存在一些技术问题,我无法使用
DISTINCT
GROUP BY
。@RamizRaja我给了你一个不需要这两个选项的选项。只是好奇,postgres是否有
存在,Postgres有一个
EXISTS
子句,q.v。这可能是另一种不使用
GROUP BY
DISTINCT
的方法。这只适用于某些版本的MySQL,即使这样也不理想,因为您不确定每个
id
值将选择哪些记录。这只适用于某些版本的MySQL,即使这样,它也不理想,因为您不确定每个
id
值将选择哪些记录。