Mysql查询-组函数的无效使用

发布于 2024-09-16 14:41:09 字数 4872 浏览 6 评论 0原文

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
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
");

Hello.. This tells me, that it is "invalid use of group function" .. what I want to do is to make sure, that when I count my log for clicks and shows of the banner, there has to be fewer rows with the bannerid=b.id in the log, than the fields b.maxklik and b.maxhits says (so I can set e.g. 6000 clicks or 50000 shows for a banner)..

Can you help with a mysql query that should work??

EDIT 2:
Same error

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

EDIT 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事和酒 2024-09-23 14:41:09

您不能在 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 变量合并到查询中...

You can't use aggregate functions in the WHERE clause - only the HAVING. I did my best to re-write your query as:

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

You appear to have a syntax error here:

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

I don't know how you want to incorporate the $ubid variable into the query...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文