基于子查询获取表中的公共值

93ze6v8z  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(300)

我找不到我要找的东西,尽管在sql中这似乎是一件“简单”的事情。
所以在这里,
我有一张包含日期和国家的表,我们称之为a。
我有一个子查询,它从表c中选择一个国家列表,我们称之为b。
我们的目标是只返回a中每个国家都在b中的日期
例如,

A                              B (SELECT FROM C WHERE ...)
date         country           country
2020-07-21   1                 1
2020-07-21   2                 2
2020-07-12   1
2020-07-12   2
2020-07-06   1
2020-07-06   2
2020-07-06   3

你应该回来

date
2020-07-21
2020-07-12
2020-07-06

但如果b是

B
country
1
2
3

那它应该回来了

2020-07-06

关键是要把所有的日期都列在b名单上
我试过了

SELECT DISTINCT T.date FROM (
    SELECT date
    FROM A
    WHERE country = ALL (SELECT country FROM C WHERE ...)
) AS T

但是它没有返回任何东西,因为我认为它只返回列表,如果它对所有内容都是真的。
这是一个mcre:

/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `countries` (
  `country_id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `projects` (
  `project_id` int(11) NOT NULL AUTO_INCREMENT,
  `project_date` date NOT NULL,
  `country_id` int(11) NOT NULL,
  PRIMARY KEY (`project_id`),
  KEY `country_id` (`country_id`),
  CONSTRAINT `projects_ibfk_1` FOREIGN KEY (`country_id`) REFERENCES `countries` (`country_id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

INSERT INTO countries VALUES (1);
INSERT INTO countries VALUES (2);
INSERT INTO countries VALUES (3);
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-21', 1);
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-21', 2);
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-12', 1)
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-12', 2)
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-06', 1);
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-06', 2);
INSERT INTO projects(project_date, country_id) VALUES ('2020-07-06', 3);

这就是我想做的

SELECT project_id, project_date
from projects
where country_id = all (select country_id from countries where country_id in (1,2))
group by project_date

我知道子查询在这里是无用的,但实际上,条件与 IN (1,2) 但这简化了它,同时又不失去我要做的事情的意义。
在这个数据库中,子查询中有(1,2),它应该返回每个日期,因为它们都有国家/地区标识1和2。但是,如果将其设置为(1,2,3),则只应将“2020-07-06”作为一个组返回,因为它是唯一具有国家/地区id 1,2和3的日期

hujrc8aj

hujrc8aj1#

假设您的查询返回 C 是这样的:

select country_id from countries where country_id in (....)

然后像这样使用:

select project_date
from projects
where country_id in (select country_id from countries where country_id in (...))
group by project_date
having count(*) = (select count(*) from countries where country_id in (...))

如果有重复的国家 projects 那就换衣服 HAVING 条款至:

having count(distinct country_id) = (select count(*) from countries where country_id in (...))

请看演示。

yhuiod9q

yhuiod9q2#

你可以试试下面的方法-

select date
from tablename 
group by date
having count(distinct country)=(select count(distinct country) from countryTable)

相关问题