问题远比标题复杂
我有4张桌子
zer_dataset,
zer_emails,
zer_addresses,
zer_privacy_entities,
zer_datasets 1:n zer_emails,
zer_datasets 1:n zer_addresses,
zer_privacy_entities 1:n zer_emails,
zer_privacy_entities 1:n zer_addreses
zer_privacy_entities是电子邮件类型或地址类型
zer_privacy_entities有两列privacy_till和privacy_updated_on
我正在尝试获取连接到单个数据集(通过zer_emails和zer_addresses)的所有privacy_entities的最大privacy_entities.privacy_updated_on,但是如果没有值(没有privacy_entity或privacy_entity.privacy_update_on的值未设置),则返回数据集.privacy_updated_on
只要可以成功实现请求。
但是现在出现了复杂的部分: 我需要获取privacy_entity的privacy_entity.privacy_till(或datasets.privacy_till),并使其具有上面检索到的max(privacy_updated_on)(在同一查询中)
那是我现在的查询
SELECT zer_datasets.id,
???? as "privacy_till",
if (
greatest(
max(p1.privacy_updated_on), max(p2.privacy_updated_on)
) is not null,
greatest(
max(p1.privacy_updated_on), max(p2.privacy_updated_on)
),
zer_datasets.privacy_updated_on
) as "privacy_updated_on"
FROM `zer_datasets`
left join zer_emails on zer_emails.dataset_id = zer_datasets.id
left join zer_privacy_entities as p1 on p1.id = zer_emails.privacy_entity_id
left join zer_addresses on zer_addresses.dataset_id = zer_datasets.id
left join zer_privacy_entities as p2 on p2.id = zer_addresses.privacy_entity_id
group by zer_datasets.id
zer_emails:
| id | dataset_id | privacy_entity_id |
|----|------------|-------------------|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
zer_addresses
| id | dataset_id | privacy_entity_id |
|----|------------|-------------------|
| 1 | 1 | 3 |
| 2 | 1 | 4 |
此数据集的privacy_entities:
| id | type | privacy_till | privacy_updated_on |
|----|------|---------------------|---------------------|
| 1 | 1 | 2019-03-07 23:00:01 | 2018-03-01 00:00:00 |
| 2 | 1 | 2019-05-07 00:00:00 | 2018-01-01 00:00:00 |
| 3 | 2 | 2018-05-25 23:00:00 | 2018-08-07 13:15:15 | // this one
| 4 | 2 | 2018-05-25 12:00:00 | 2018-02-01 00:00:00 |
预期结果(2018-08-07 13:15:15是最大的privacy_updated_on,->我得到2018-05-25 23:00:00作为privacy_till,因为它对应于privacy_updated_on)
| id | privacy_till | privacy_updated_on |
|----|---------------------|---------------------|
| 1 | 2018-05-25 23:00:00 | 2018-08-07 13:15:15 |
没有设法找到一种有效的方法(> 10.000.000数据集,> 35.000.000 privacy_entities)
谢谢!