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 7055827
In Process

The Archive Base Latest Questions

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

I have this assets table: asset_id | mediumint(8) asset_type_id | mediumint(8) asset_name | varchar(200)

  • 0

I have this assets table:

asset_id      | mediumint(8)
asset_type_id | mediumint(8)
asset_name    | varchar(200)

and a lookup table:

product_id | mediumint(8)
asset_id   | mediumint(8)
sequence   | smallint(3)

Basically, the lookup table can have multiple entries of assets for an item. And you can get the item description in assets table.

The assets is categorized as an image, video, sound, etc based on its asset_type_id. This asset_type_id is comprises of integers 1 to 16.

Now, I want to count the number of images(1), video(2) and sound(3) ONLY for a particular item. Is there anyway I can do this using one SELECT QUERY?

I can’t limit my selection to just asset_type_id = 1 or 2 or 3 because I still need the other assets data.

  • 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-28T03:47:43+00:00Added an answer on May 28, 2026 at 3:47 am
    SELECT 
       l.product_id, t.asset_type_id, COUNT(*)
    FROM
       lookup l
       CROSS JOIN
       (SELECT 1 AS asset_type_id UNION ALL SELECT 2 UNION ALL SELECT 3) t
       LEFT JOIN
       assets a ON l.asset_id = a.asset_id AND t.asset_type_id = a.asset_type_id
    GROUP BY
       l.product_id, t.asset_type_id;
    

    Edit: as separate columns

    SELECT 
       l.product_id, 
       COUNT(CASE WHEN a.asset_type_id = 1 THEN 1 END) AS asset_count_image,
       COUNT(CASE WHEN a.asset_type_id = 2 THEN 1 END) AS asset_count_video,
       COUNT(CASE WHEN a.asset_type_id = 3 THEN 1 END) AS asset_count_sound
    FROM
       lookup l
       LEFT JOIN
       assets a1 ON l.asset_id = a.asset_id AND a.asset_type_id IN (1, 2, 3)
    GROUP BY
       l.product_id;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a database named crea with a table named assets and in this
I have a database named crea with a table named assets and in this
I have this method call I have to use... financial_document.assets.length But financial_document.assets could be
I have a .js.erb file in my assets directory. This reads some locale specific
I have this XML <data> <peptides> <peptide> <accession>111</accession> <sequence>AAA</sequence> <score>4000</score> </peptide> <peptide> <accession>111</accession> <sequence>AAA</sequence>
I have two tables. One table contains information about Assets, another Table about their
I have a function that searches a database table via ajax. This is the
I have this table: id | owner | asset | rate ------------------------- 1 |
I have the following FluentNHibernate mapping: public AssetMap() { Table(PRASSET); Id(x => x.Id).Column(PRA_RECNUM).GeneratedBy.Sequence(PRA_RECNUM_GEN); ...fields
I have 2 tables: City table - columns CityID , Name , Period Assets

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.