Mysql全外连接

标签 mysql

我有一个名为“电子邮件”的表:

     email          domain         timestamp
abcd@google.com    google.com      2019/12/08
abd@google.com     google.com      2019/12/08
abcd@google.com    google.com      2019/12/09
abd@google.com     google.com      2019/12/09
123@google.com     google.com      2019/12/09

我只想输出与上一个日期相比新的电子邮件,如下所示:

       email        domain      timestamp
123@google.com     google.com   2019/12/09

因为 mysql 没有完整的外部连接,我正在使用以下查询,但它不起作用:

 select * from email e1
 left join emails e2
 on e1.domain_searched = e2.domain_searched 
 where
 e1.timestamp = '2019/12/08'

 union

 select * from email e1
 right join emails e2
 on e1.domain_searched = e2.domain_searched 
 where
 e1.timestamp = '2019/12/09';

伙计们,我需要你的建议......提前谢谢你......

最佳答案

i am looking for only emails that do not have timestamp prior in my db or in other way, it was not in my db at all before

NOT EXISTS:

select * from emails e
where e.timestamp = '2019/12/09'
and not exists (
  select 1 from emails
  where email = e.email and timestamp < e.timestamp
);  

参见 demo .
结果:

| email          | domain     | timestamp  |
| -------------- | ---------- | -----------|
| 123@google.com | google.com | 2019-12-09 |

关于Mysql全外连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59255483/

相关文章:

mysql - 如何按mysql中的特定值对结果进行排序

mysql - 如果变量前有连字符,则 REGEXP 不匹配

mysql - 使用特定列名将 MySQL 表值复制到重复表

php - Codeigniter Select_Sum 返回 "array"不是数值?

PHP 脚本优化,用于使用来自 FTP 摄像头的文件实时更新数据库 mysql 的文件名数据

mysql - MySQL触发器中的多个if语句

Mysql Join查询使用group by

MySQL根据另一个表中的日期计算一个表中的行数

php - 无法使用关联数组通过 PDO 插入数据

php - 删除然后插入偶尔会因重复键而失败