Jump to content

[SOLVED] Counting data using 3 tables (table 1 & 2 and 2 & 3 have ID in common)


Recommended Posts

I'm trying to count the number of products in each genre where the genre's parent id equals 0 for each shop. I have 3 tables: products, genres, and shops.

 

A product has a shop id and a genre id. A store has a store id. A genre has a genre id and parent id.

 

What I would like:

+----------+----------+---------+

| Shop ID | Genre ID | Count(*) |

+----------+----------+---------+

|        1 |        1 |      3 |

|        1 |        2 |      5 |

|        1 |        3 |      0 |

|        2 |        1 |      1 |

|        2 |        2 |      0 |

|        2 |        3 |      3 |

Etc...

+----------+----------+---------+

 

My attemp:

SELECT products.genre_id, COUNT(*), shops.id as 'Shop ID' FROM products LEFT JOIN genres ON (genres.genre_id = products.genre_id) LEFT JOIN shops ON (products.shop_id = shops.id) WHERE genres.parent_id = '0' GROUP BY shops.id;

 

Which produces:

+----------+----------+------+

| genre_id | COUNT(*) | Shop ID  |

+----------+----------+------+

|        1    |        2    |    1 |

|        1    |        2    |    2 |

|        3    |        3    |    3 |

+----------+----------+------+

 

which is wrong. For example shop 1 has products in more than just genre 1... Any help would be great, thank you!

 

The 3 tables:

Products

+----------+--------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+----------+--------------+------+-----+---------+-------+

| id      | int(11)      | NO  | PRI |        |      |

| shop_id  | int(11)      | NO  |    |        |      |

| genre_id | int(11)      | NO  |    |        |      |

| title    | varchar(255) | NO  |    |        |      |

+----------+--------------+------+-----+---------+-------+

 

Genres

+-----------+--------------+------+-----+---------+-------+

| Field    | Type        | Null | Key | Default | Extra |

+-----------+--------------+------+-----+---------+-------+

| genre_id  | int(11)      | NO  | PRI |        |      |

| parent_id | int(11)      | NO  |    | 0      |      |

| name      | varchar(100) | NO  |    |        |      |

+-----------+--------------+------+-----+---------+-------+

 

Shops

+-------+--------------+------+-----+---------+----------------+

| Field | Type        | Null | Key | Default | Extra          |

+-------+--------------+------+-----+---------+----------------+

| id    | int(11)      | NO  | PRI | NULL    | auto_increment |

| name  | varchar(100) | NO  |    |        |                |

+-------+--------------+------+-----+---------+----------------+

 

MySQL Server version: 5.0.37

That works, thank you!

 

Out of curiosity, is there anywhere to display a count of zero, so it always displays all genres?

 

Like this:

+----------+----------+---------+

| Shop ID | Genre ID | Count(*) |

+----------+----------+---------+

|        1 |        1 |      3 |

|        1 |        2 |      5 |

|        1 |        3 |      0 |

|        2 |        1 |      1 |

|        2 |        2 |      0 |

|        2 |        3 |      3 |

Etc...

+----------+----------+---------+

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.