选择问题中的Mysql子查询

时间:2015-12-03 10:18:41

标签: mysql sql

我对此查询有疑问:

SELECT ea.date AS date,
            ea.locale AS geo,
            ea.adv_id AS adv_id,
            adv.name AS adv_name,
            ea.srcid AS srcid,
            (IFNULL(
                (SELECT g.pub_rate FROM rates_pub AS g WHERE g.adv_id = ea.adv_id AND g.pub_id=ea.pub_id AND g.geo=ea.locale g.created_on = (SELECT MAX(t.created_on) FROM rates_pub t WHERE t.adv_id = ea.adv_id AND t.pub_id = ea.pub_id AND t.geo = ea.locale AND t.created_on <= ea.date)),
                0
            )),

            sum(ea.count) AS count
            FROM events_aggr AS ea
            LEFT JOIN lookups.advertisers adv
            ON adv.id = ea.adv_id
            WHERE ea.date >= '2015-12-01' AND ea.date <= '2015-12-02' AND event = 5
            GROUP BY ea.date,ea.adv_id,ea.srcid,ea.locale
            ORDER BY ea.date,ea.adv_id,ea.locale;

我收到错误信息:

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'g.created_on =(SELECT MAX(t.created_on)FROM rates_pub t WHERE t.adv_id = ea.ad'第7行附近使用正确的语法

你有什么想法,为什么它不起作用

感谢,

2 个答案:

答案 0 :(得分:1)

您似乎错过了AND

(IFNULL(
        (SELECT g.pub_rate FROM rates_pub AS g 
            WHERE g.adv_id = ea.adv_id 
            AND g.pub_id=ea.pub_id 
            AND g.gea=ea.locale 
    here ????? g.created_on = (SELECT MAX(t.created_on) 
                   FROM rates_pub t  
                   WHERE t.adv_id = ea.adv_id 
                   AND t.pub_id = ea.pub_id 
                   AND t.geo = ea.locale 
                   AND t.created_on <= ea.date)),
            0
        )), .......

答案 1 :(得分:1)

你错过了什么

g.gea=ealocale g.created_on