Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1038423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:58:25+00:00 2026-05-16T14:58:25+00:00

mysql_query( SELECT b.id as b_id FROM banner b INNER JOIN bannerhits bl ON b.id

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

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:58:25+00:00Added an answer on May 16, 2026 at 2:58 pm

    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…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');
I have this query: SELECT a.id, b.discount FROM (a LEFT JOIN b ON b.id_a
I'm having trouble optimizing this query: SELECT a.id FROM a JOIN b ON a.id=b.id
$result = mysql_query(SELECT * FROM media WHERE path = '$target'); if($row = mysql_num_rows($result)==1) {
$query = mysql_query(SELECT * FROM news WHERE id = '{$_GET['id']}'); $news = mysql_fetch_assoc($query); $sql84
include(conn.php); $result = mysql_query(SELECT * FROM sggame); while($row = mysql_fetch_assoc($result)); { $id = $row['id'];
I'm getting odd results from a MySQL SELECT query involving a LEFT JOIN ,
$query = SELECT * FROM `table`; $results = mysql_query($query, $connection); If 'table' has no
if (isset($_POST['login'])) { $query = mysql_query(" SELECT id FROM users WHERE username = '".mysql_real_escape_string($_POST['username'])."'
function get_total_adults() { $sql = SELECT SUM(number_adults_attending) as number_of_adults FROM is_nfo_rsvp; $result = mysql_query($sql)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.