我有一个“User”表,其中包含UserId和addedDate字段。
我需要获取用户数据 - 在添加 日期之前的上个月和当月添加,而不是在系统中添加90天。
用户表
13:00:28,442 INFO [org.springframework.web.context.ContextLoader] (ServerService Thread Pool -- 29) Root WebApplicationContext: initialization completed in 1027 ms
13:00:28,445 INFO [io.undertow.servlet] (ServerService Thread Pool -- 29) Initializing Spring FrameworkServlet 'dispatcher'
13:00:28,445 INFO [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 29) FrameworkServlet 'dispatcher': initialization started
13:00:28,447 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 29) Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Jul 17 13:00:28 NPT 2017]; parent: Root WebApplicationContext
13:00:28,447 INFO [org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor] (ServerService Thread Pool -- 29) JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13:00:28,462 INFO [org.springframework.web.servlet.DispatcherServlet] (ServerService Thread Pool -- 29) FrameworkServlet 'dispatcher': initialization completed in 17 ms
13:00:28,462 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 29) WFLYUT0021: Registered web context: /taml-nbl
13:00:28,494 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0010: Deployed "taml-nbl.war" (runtime-name : "taml-nbl.war")
13:00:30,609 INFO [stdout] (default task-21) BfiDAOImplementation:bfiLogo method invoked.
13:00:33,533 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0019: Stopped Driver service with driver-name = taml-nbl.war_org.postgresql.Driver_9_4
13:00:33,538 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 28) WFLYUT0022: Unregistered web context: /taml-nbl
13:00:33,542 INFO [io.undertow.servlet] (ServerService Thread Pool -- 28) Destroying Spring FrameworkServlet 'dispatcher'
13:00:33,542 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 28) Closing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Mon Jul 17 13:00:28 NPT 2017]; parent: Root WebApplicationContext
13:00:33,546 INFO [io.undertow.servlet] (ServerService Thread Pool -- 28) Closing Spring root WebApplicationContext
13:00:33,546 INFO [org.springframework.web.context.support.AnnotationConfigWebApplicationContext] (ServerService Thread Pool -- 28) Closing Root WebApplicationContext: startup date [Mon Jul 17 13:00:27 NPT 2017]; root of context hierarchy
13:00:33,547 INFO [org.springframework.context.support.DefaultLifecycleProcessor] (ServerService Thread Pool -- 28) Stopping beans in phase 2147483647
13:00:33,550 INFO [org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler] (ServerService Thread Pool -- 28) Stopping...
13:00:33,552 INFO [org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler] (ServerService Thread Pool -- 28) BrokerAvailabilityEvent[available=false, SimpleBrokerMessageHandler [DefaultSubscriptionRegistry[cache[0 destination(s)], registry[0 sessions]]]]
13:00:33,553 INFO [org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler] (ServerService Thread Pool -- 28) Stopped.
13:00:33,555 INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] (ServerService Thread Pool -- 28) Shutting down ExecutorService 'brokerChannelExecutor'
13:00:33,564 INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler] (ServerService Thread Pool -- 28) Shutting down ExecutorService 'messageBrokerTaskScheduler'
13:00:33,565 INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] (ServerService Thread Pool -- 28) Shutting down ExecutorService 'clientOutboundChannelExecutor'
13:00:33,565 INFO [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] (ServerService Thread Pool -- 28) Shutting down ExecutorService 'clientInboundChannelExecutor'
13:00:33,597 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0028: Stopped deployment taml-nbl.war (runtime-name: taml-nbl.war) in 67ms
13:00:33,606 INFO [org.jboss.as.server] (DeploymentScanner-threads - 1) WFLYSRV0009: Undeployed "taml-nbl.war" (runtime-name: "taml-nbl.war")
13:00:38,634 INFO [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 2) WFLYDS0004: Found taml-nbl.war in deployment directory. To trigger deployment create a file called taml-nbl.war.dodeploy
我需要输出:
Userid addedDate enddate
001 2017-07-01 2017-09-05
002 2017-02-25 2017-02-01
003 2017-06-01 2017-09-21
003 2017-04-25 2017-05-29
004 2017-06-01 2017-09-21
005 2017-06-05 2017-09-07
005 2017-01-01 2017-01-31
此处不需要003,因为它不是新用户(在添加 日期之前90天的可用系统)。在最后一只飞蛾或当前飞蛾中没有加入002,所以这也不能作为新用户考虑。
我已尝试过以下查询,但效果不佳:
userid
001
004
005
请提供一些建议。
答案 0 :(得分:0)
您需要使用INNER
查询来获得结果:
SELECT *
FROM users
WHERE addedDate >= DATE_ADD(NOW(), INTERVAL -2 MONTH)
AND userid NOT IN (
SELECT userid
FROM users
WHERE addedDate < DATE_ADD(NOW(), INTERVAL -2 MONTH)
);
有几点:
003
在系统中addedDate
前90天不可用(2017-06-01 vs 2017-04-25,即37天),所以它可能与选择标准相同(即不是本月或上个月)?005
在addedDate
之前90天(或上个月)可用,因此不应该提取这是 SQL Fiddle 。