views:

27

answers:

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
+1  A: 

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...

OMG Ponies
It is still the same error :(
Dennis Lauritzen
@Dennis Lauritzen: I missed one - updated, but I'm skeptical it will return what you expect...
OMG Ponies
Can you ORDER BY a column that is not included in the SELECT clause? I'm looking at the "ORDER BY remainingshow ASC" fragment.
Jim Tough
well, that remainingshow was a mistake - but it didn't solve the problem..
Dennis Lauritzen
@Jim Tough: Tested, MySQL 4.1 does **not** have an issue with ordering by a column not in the SELECT list...
OMG Ponies
I think your updated version is quite okay.. It'll take 5 minutes to test.. Thanks in advance
Dennis Lauritzen
Well, there is a bit of a problem.. I have created a banner with "b.maxklik", "b.maxhits" and "b.maxviewsprday" = 0. And set b.remainingviews = 1... everything else is not set.. Shouldn't it return a row then !? [Look at EDIT 3 in original post for code]
Dennis Lauritzen
@Dennis Lauritzen: I'd prefer that if there's no more query errors, that you create a new question to address the data issue(s) you're seeing.
OMG Ponies
I will, thank you very much !
Dennis Lauritzen