我有一个mysql表,每天都在这里加载生产数据。我需要一个列值(引用),它不存在于较早的日期,即当前日期-1
我曾尝试过不同的问题:
1)
select distinct referral from time_in_report where crt_date=curdate() and referral not in(select distinct referral from time_in_report where crt_date<curdate())上面的查询是正确的,但是,由于表中有大量数据,因此响应和内存耗尽需要很长时间。所以我不能使用上面的查询
2)
select distinct referral from time_in_report a where a.crt_date=curdate() and not exist(select 1 from time_in_report b where a.id=b.id and b.crt_date<curdate())在上面的查询中,ID列是表中的主键。因为一个引用可以有不同的ID,所以上面的查询结果是不正确的。我得到一些参考,这是存在于以前的日期,而不是目前的日期。
在我的mysql版本中,减号不起作用。
请提出你的想法,作为如何获得当前日期的明确推荐,除了当前日期以外,其他日期不存在。
提前谢谢
发布于 2015-01-14 17:21:32
您可以使用不等于当前日期而不是减去当前日期来检查该字段。
select distinct referral from time_in_report where crt_date=curdate() and referral not in(select distinct referral from time_in_report where crt_date!=curdate());试试这个。
发布于 2015-01-15 10:29:51
mysql显示的效率低下的原因是因为表中可能缺少您在设计过程中没有提供的索引,而MySQL优化器由于派生的表而没有产生有效的计划。
select distinct a.referral from time_in_report a left join time_in_report b
on
a.referal_basename=b.referal_basename where a.crt_date=curdate() and b.crt_date<curdate() 。
以上查询可能有效,也可能不起作用,但您只需尝试一下。它可以替代mysql中的负查询,因为mysql中不存在减号操作符。这很奇怪,而且是真的。
如果您有大量的数据,那么您应该有索引,以便可以通过使用not in query来检索数据。
https://stackoverflow.com/questions/27948637
复制相似问题