kuma Posted December 4, 2007 Share Posted December 4, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/80128-solved-counting-data-using-3-tables-table-1-2-and-2-3-have-id-in-common/ Share on other sites More sharing options...
fenway Posted December 4, 2007 Share Posted December 4, 2007 You need "GROUP BY shops.id, genre.id". Quote Link to comment https://forums.phpfreaks.com/topic/80128-solved-counting-data-using-3-tables-table-1-2-and-2-3-have-id-in-common/#findComment-406129 Share on other sites More sharing options...
kuma Posted December 4, 2007 Author Share Posted December 4, 2007 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... +----------+----------+---------+ Quote Link to comment https://forums.phpfreaks.com/topic/80128-solved-counting-data-using-3-tables-table-1-2-and-2-3-have-id-in-common/#findComment-406160 Share on other sites More sharing options...
fenway Posted December 4, 2007 Share Posted December 4, 2007 It will display zero counts for any genres that are in the table... it doesn't know what "all" means... you could work around this, by why? Quote Link to comment https://forums.phpfreaks.com/topic/80128-solved-counting-data-using-3-tables-table-1-2-and-2-3-have-id-in-common/#findComment-406178 Share on other sites More sharing options...
kuma Posted December 4, 2007 Author Share Posted December 4, 2007 It's not really needed, just curious how to manipulate the results. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/80128-solved-counting-data-using-3-tables-table-1-2-and-2-3-have-id-in-common/#findComment-406183 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.