Mysql查询 - 无效使用组函数

时间:2010-08-29 22:30:08

标签: sql mysql mysql-error-1111

mysql_query("
            SELECT
                b.id as b_id
            FROM
                banner b
            INNER JOIN
                bannerhits bl
                    ON
                        b.id = bl.bannerid
                        AND
                        bl.userid = '".$this->userid."'
            INNER JOIN
                bannerhits blog
                    ON
                        b.id = blog.bannerid
            INNER JOIN
                bannerklik bk
                    ON
                        b.id = bk.bannerid
            WHERE
                (
                b.placement = '".$place."'
                AND
                    (
                    b.usertype = '".$usertype."'
                    OR
                    b.usertype = ''
                    )
                AND
                b.userpostalcode LIKE ',".$postcode.",'
                AND
                    (
                        b.userage LIKE ',".$ageYears.",'
                    OR
                        b.userage IS NULL
                    )
                AND
                    (
                    b.maxviewsprday > count(bl.id)
                    OR
                    b.maxviewsprday IS NULL
                    )
                AND
                b.maxhits > count(blog.id)
                AND
                b.maxklik > count(bk.id)
                ".$ubid."
                )
                OR
                remainingshow = '1'
            GROUP BY
                bl.id,
                bk.id,
                blog.id
            ORDER BY
                remainingshow ASC
            LIMIT 1
");

你好..这告诉我,它是“无效使用组功能”..我想要做的是确保,当我计算我的日志点击和显示横幅时,必须有日志中bannerid = b.id的行数少于b.maxklik和b.maxhits所示的字段(因此我可以为横幅设置例如6000次点击或50000次显示)..

你能帮助一个应该工作的mysql查询吗?

编辑2: 同样的错误

SELECT
                bl.id as bl_id,
                bk.id as bk_id,
                blog.id as blog_id
            FROM
                banner b
            INNER JOIN
                bannerhits bl
                    ON
                        b.id = bl.bannerid
                        AND
                        bl.userid = '".$this->userid."'
            INNER JOIN
                bannerhits blog
                    ON
                        b.id = blog.bannerid
            INNER JOIN
                bannerklik bk
                    ON
                        b.id = bk.bannerid
            WHERE
                (
                b.placement = '".$place."'
                AND
                    (
                    b.usertype = '".$usertype."'
                    OR
                    b.usertype = ''
                    )
                AND
                b.userpostalcode LIKE ',".$postcode.",'
                AND
                    (
                        b.userage LIKE ',".$ageYears.",'
                    OR
                        b.userage IS NULL
                    )
                AND
                    (
                    b.maxviewsprday > count(bl.id)
                    OR
                    b.maxviewsprday IS NULL
                    )
                AND
                b.maxhits > count(blog.id)
                AND
                b.maxklik > count(bk.id)
                ".$ubid."
                )
                OR
                remainingshow = '1'
            GROUP BY
                b.id,
                bl.id,
                bk.id,
                blog.id
            ORDER BY
                remainingshow ASC
            LIMIT 1

编辑3:

SELECT
    b.id as b_id,
    b.maxhits as b_maxhits,
    b.maxklik as b_maxkli,
    b.maxviewsprday as b_maxviewsprday
FROM banner b
    JOIN
        bannerhits bl
            ON
                b.id = bl.bannerid
                AND
                bl.userid = '".$this->userid."'
    JOIN
        bannerhits blog
            ON
                b.id = blog.bannerid
    JOIN
        bannerklik bk
            ON
            b.id = bk.bannerid
    WHERE
        (
            b.placement = '".$place."'
            AND
            b.usertype IN ('".$usertype."', '')
            AND
            b.userpostalcode LIKE ',".$postcode.",'
            AND
                (
                b.userage LIKE ',".$ageYears.",'
                OR
                b.userage IS NULL
                )
            AND
            b.maxviewsprday IS NULL
        )
        OR
        b.remainingshow = '1'
    GROUP BY
        bl.id,
        bk.id,
        blog.id
    HAVING
        (b.maxhits > count(blog.id) OR b.maxhits = '0')
        AND
        (b.maxklik > count(bk.id) OR b.maxklik = '0')
        AND
        (b.maxviewsprday > count(bl.id) OR b.maxviewsprday = '0')
    ORDER BY
        b.remainingshow ASC
    LIMIT
        1

1 个答案:

答案 0 :(得分:2)

您不能在WHERE子句中使用聚合函数 - 只能使用HAVING。我尽力将您的查询重写为:

SELECT b.id as b_id
  FROM BANNER b
  JOIN BANNERHITS bl ON b.id = bl.bannerid
                    AND bl.userid = '".$this->userid."'
  JOIN BANNERHITS blog ON b.id = blog.bannerid
  JOIN BANNERKLIK bk ON b.id = bk.bannerid
 WHERE (    b.placement = '".$place."'
        AND b.usertype IN ('".$usertype."', '')
        AND b.userpostalcode LIKE ',".$postcode.",'
        AND (b.userage LIKE ',".$ageYears.",' OR b.userage IS NULL)
        AND b.maxviewsprday IS NULL)
    OR remainingshow = '1'
GROUP BY bl.id, bk.id, blog.id
  HAVING b.maxhits > count(blog.id)
     AND b.maxklik > count(bk.id)
     AND b.maxviewsprday > count(bl.id) 
ORDER BY remainingshow ASC
   LIMIT 1

这里似乎有语法错误:

AND b.maxklik > count(bk.id) ".$ubid.")

我不知道你想如何将$ ubid变量合并到查询......