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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:08:44+00:00 2026-06-16T17:08:44+00:00

Having a hard time trying to work out how to make a mysql query

  • 0

Having a hard time trying to work out how to make a mysql query and output the data into a table like the one I have made below.

Been playing around for a few days now with no luck, so any help would be much appreciated.

The image below shows my 3 database tables(in simple form) and what i’m trying to output using PHP/MYSQL.

The image also shows the total amount (count) of listings for each user for each category, I cant work out how i join it all together.

Hope this makes some sense!

http://img.photobucket.com/albums/v432/Winegums/table_zps2b57f483.jpg

  • 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-06-16T17:08:45+00:00Added an answer on June 16, 2026 at 5:08 pm

    Initially, you can do it like this,

    SELECT  a.user_name,
            SUM(CASE WHEN c.cat_name = 'books' THEN 1 ELSE 0 END) books,
            SUM(CASE WHEN c.cat_name = 'videos' THEN 1 ELSE 0 END) videos
    FROM    users a 
            LEFT JOIN downlods b
                ON a.user_ID = b.user_ID
            LEFT JOIN download_cats c
                ON b.cat_id = c.cat_id
    GROUP BY a.user_name
    

    But I guess you have unknown number of cat_name on your table. A Dynamic SQL is much more preferred,

    SET @sql = NULL;
    SELECT
      GROUP_CONCAT(DISTINCT
        CONCAT(
          'SUM(CASE WHEN c.cat_name = ''',
          cat_name,
          ''' THEN 1 ELSE 0 END) AS ',
          cat_name
        )
      ) INTO @sql
    FROM download_cats;
    
    SET @sql = CONCAT(' SELECT  a.user_name, ', @sql, ' 
                        FROM    users a 
                                LEFT JOIN downlods b
                                    ON a.user_ID = b.user_ID
                                LEFT JOIN download_cats c
                                    ON b.cat_id = c.cat_id
                        GROUP BY a.user_name');
    
    PREPARE stmt FROM @sql;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having hard time trying to figure out how to auto-save user data in
I am having a hard time trying to subset a data.table (package) in R.
I am having one heck of a hard time trying to figure this out.
I am starting out with mongodb and having hard time trying to query nested
I'm having a hard time trying to make this work. I'm trying to execute
I am having a hard time trying to figure out which data type I
I'm having a hard time trying to get hasMany to work in Grails 2.0.1
I'm having a real hard time trying to figure out how to fix this
I'm currently trying to get into jQuery. And I'm having a hard time (-;
I am having a really hard time trying to get this to work. All

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.