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

  • SEARCH
  • Home
  • 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 7398777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:54:47+00:00 2026-05-29T03:54:47+00:00

Please help. I’m trying to update this one table with the current count of

  • 0

Please help. I’m trying to update this one table with the current count of assets our products have. If the product already exist in the table, it should update the most updated count of the product. However, using the query below, mysql is returning me

“ERROR 1111 (HY000): Invalid use of group function”.

I can’t determine what’s my error or if it is really valid to use count in function ‘on duplicate key’:

INSERT INTO report_count_assets
    SELECT products.product_id, 
    count(product_assets.asset_id),
    count(case when assets.asset_type_id=1 THEN 1 END), 
    count(case when assets.asset_type_id=2 THEN 1 END), 
    count(case when assets.asset_type_id=3 THEN 1 END), 
    count(case when assets.asset_type_id=11 THEN 1 END) 
    FROM products 
    LEFT JOIN product_assets USING (product_id) 
    LEFT JOIN assets USING (asset_id)
    WHERE products.brand_id=671

ON DUPLICATE KEY UPDATE
    asset_count = count(product_assets.asset_id),
    asset_type_image = count(case when assets.asset_type_id=1 THEN 1 END), 
    asset_type_video = count(case when assets.asset_type_id=2 THEN 1 END), 
    asset_type_sound = count(case when assets.asset_type_id=3 THEN 1 END), 
    asset_type_install = count(case when assets.asset_type_id=11 THEN 1 END);
  • 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-29T03:54:48+00:00Added an answer on May 29, 2026 at 3:54 am

    I don’t think you can use aggregate functions in the ON DUPLICATE. MySQL sees your SQL sort of like this:

    insert into report_count_assets
    expr
    on duplicate key update
    ...
    

    The ON DUPLICATE doesn’t know what’s going on in expr, it only knows that it has a single duplicate row to deal with. Without knowing what’s going on inside expr, there is no context for the counts to operate on. Also, you’re supposed to use values in the UPDATE:

    You can use the VALUES(col_name) function in the UPDATE clause to refer to column values from the INSERT portion of the INSERT ... ON DUPLICATE KEY UPDATE statement.

    And values(count(x)) is not valid syntax. But values(column_name) is valid so this should work:

    INSERT INTO report_count_assets
    (product_id, asset_count, asset_type_image, asset_type_video, asset_type_sound, asset_type_install)
        SELECT products.product_id, 
        count(product_assets.asset_id),
        count(case when assets.asset_type_id=1 THEN 1 END), 
        count(case when assets.asset_type_id=2 THEN 1 END), 
        count(case when assets.asset_type_id=3 THEN 1 END), 
        count(case when assets.asset_type_id=11 THEN 1 END) 
        FROM products 
        LEFT JOIN product_assets USING (product_id) 
        LEFT JOIN assets USING (asset_id)
        WHERE products.brand_id=671
    ON DUPLICATE KEY UPDATE
        asset_count = values(asset_count),
        asset_type_image = values(asset_type_image), 
        asset_type_video = values(asset_type_video), 
        asset_type_sound = values(asset_type_sound), 
        asset_type_install = values(asset_type_install);
    

    I had to guess the name of the product_id column in report_count_assets.

    If that doesn’t work (as apparently it doesn’t), then you can do it the hard way by precomputing the SELECT. Create a temporary table:

    create temporary table t (
        product_id int,
        product_count int,
        assets1 int,
        assets2 int,
        assets3 int,
        assets11 int
    )
    

    Populate it:

    INSERT INTO t (product_id, product_count, assets1, assets2, assets3, assets11)
    SELECT products.product_id, 
    count(product_assets.asset_id),
    count(case when assets.asset_type_id=1 THEN 1 END), 
    count(case when assets.asset_type_id=2 THEN 1 END), 
    count(case when assets.asset_type_id=3 THEN 1 END), 
    count(case when assets.asset_type_id=11 THEN 1 END) 
    FROM products 
    LEFT JOIN product_assets USING (product_id) 
    LEFT JOIN assets USING (asset_id)
    WHERE products.brand_id=671
    

    And then use that temporary table to do the insert that you really want to do:

    insert into report_count_assets
        select product_id, product_count, assets1, assets2, assets3, assets11
        from t
    on duplicate key update
        asset_count = values(product_count),
        asset_type_image = values(assets1),
        asset_type_video = values(assets2),
        asset_type_sound = values(assets3),
        asset_type_install = values(assets11)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please help me on this problem. I have 2 identical tables, one is timetable
Please Help me to do this first i have this div <div class=loader alt=stat.php>
Please help me understand the meaning of this code. I have seen this kind
Please help me: I have two columns in mysql table - date1 (datetime) and
please help me understand this. You have a function that calls a few methods:
please help me out from this I have created a database in Blackberry phone
please help me resolve this problem: There is an ambient MSMQ transaction. I'm trying
Please help me to solve this: I have two strings for email-id and password
please help to figure this out. I have some leaking code, and I don't
Please help as I have spent two days on this... I have a JSON

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.