2012-02-13 49 views
0

我有这个疑问在这里:返回指定的值,如果没有结果

$query='insert into pageview (visitor,id_realestate,time) 
    select "q17872745t", 150, now() 
    from pageview a 
    where DATE_ADD(now(),INTERVAL -30 MINUTE) > (
     select max(time) from pageview b where a.id_realestate=b.id_realestate AND a.visitor=b.visitor 
) LIMIT 1'; 

我想这个部分:

select max(time) from pageview b where a.id_realestate=b.id_realestate AND a.visitor=b.visitor 

返回一个日期/时间这是现在( )减去40分钟如果没有结果

+0

什么是想干什么?你为什么要获得两次“pageview”表?请告诉我们你需要插入什么,我们可以正确地重构你的查询 – Marco 2012-02-13 10:32:45

+0

@Marco看到这个http://stackoverflow.com/questions/9251082/insert-with-a-condition/9251278#9251278 我现在想还插入数据如果组合(访客,价值)不存在。 – 2012-02-13 10:35:14

回答

1

MAX(time)包装在IFNULL条款中。

IFNULL(MAX(time), NOW() - INTERVAL 40 MINUTE) 
0

假设你的查询是正确的,则只需将您的当前查询的另一个查询和检查,看看是否值为null就像这样:

选择IFNULL(从浏览量b 选择MAX(时间) 其中a.id_realestate = b.id_realestate AND a.visitor = b.visitor,DATE_SUB(现在的(),INTERVAL 30分钟));

在上面的查询中,我用max()作为子查询包装了查询,并且如果返回的值为null,则使用DATE_SUB从现在()减去30分钟后返回一个值。

+0

这里不需要子查询。 – fancyPants 2012-02-13 10:59:06