如何正确编写SQL查询以选择日期

时间:2019-03-01 12:04:03

标签: mysql sql

如何编写这样的请求。我在数据库中有一个date(tableName = DateTime),id(tableName = Name)和3个表,其中id为date。

enter image description here

我需要选择ID,在该ID上其他日期不输入日期。那些。如果第3行的日期不在表中,那么我将获得id = 3 我这样写,但不正确(

 "select id from Name 
  where (select id from Name where id=3 
  and (date.DateTime not in (select date from DateTime)))"  

1 个答案:

答案 0 :(得分:0)

这是您想要的吗?

select n.id
from name n
where not exists (select 1
                  from name n2
                  where n2.date = n.date and n2.id <> n.id
                 );

等效的方法是:

select max(n.id)
from name n
group by n.date
having count(*) = 1;