holly9 Posted March 20, 2013 Share Posted March 20, 2013 Hi i spend hours on this and i have search all over but i couldn't find the right code.I already know how to count the items.below is the sql code i use to test . SELECT Items.item FROM Items LEFT OUTER JOIN ( SELECT item, COUNT( item ) FROM Members GROUP BY item )Members ON Items.item = Ad.Members i don't get the results i want with above. echo "<h4>Members table:</h4> <table border=\"1\"> <tr> <th>username</th> <th>item</th> </tr> <tr> <td>kim</td> <td>orange</td> </tr> <tr> <td>tom</td> <td>apple</td> </tr> </table> <br><br> <h4>Items table:</h4> <table border=\"1\"> <tr> <th>item</th> </tr> <tr> <td>orange</td> </tr> <tr> <td>apple</td> </tr> <tr> <td>grape</td> </tr> </table> <br><br> <h4>I want to get this table:</h4> <table border=\"1\"> <tr> <th>item</th> <th>COUNT(item)</th> </tr> <tr> <td>orange</td> <td>1</td> </tr> <tr> <td>apple</td> <td>1</td> </tr> <tr> <td>grape</td> <td></td> </tr> </table>"; Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/ Share on other sites More sharing options...
monkeypaw201 Posted March 20, 2013 Share Posted March 20, 2013 Hopefully I don't spark a great debate about this, but could you not do it programmatically instead? eg. SELECT `item` FROM `items` then do a foreach() SELECT COUNT(`item`) AS `item_count` FROM `members` WHERE `item` = '$current_item_id_from_loop' Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419738 Share on other sites More sharing options...
holly9 Posted March 20, 2013 Author Share Posted March 20, 2013 hi thanks but i wanted to avoid loops with mysql because i read that it can make the database run slower.Above is an example.I have many items in 2 tables and i'll be doing different things so there will be about 180 querys if i use loops.i have loops right now but i'm changing it. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419815 Share on other sites More sharing options...
Jessica Posted March 20, 2013 Share Posted March 20, 2013 Hopefully I don't spark a great debate about this, but could you not do it programmatically instead? eg. SELECT `item` FROM `items` then do a foreach() SELECT COUNT(`item`) AS `item_count` FROM `members` WHERE `item` = '$current_item_id_from_loop' NO! Bad. There is NO debating, that's just wrong. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419818 Share on other sites More sharing options...
Jessica Posted March 20, 2013 Share Posted March 20, 2013 OP, explain better what you're trying to do, and post the table's structures. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419819 Share on other sites More sharing options...
KevinM1 Posted March 20, 2013 Share Posted March 20, 2013 Hopefully I don't spark a great debate about this, but could you not do it programmatically instead? eg. SELECT `item` FROM `items` then do a foreach() SELECT COUNT(`item`) AS `item_count` FROM `members` WHERE `item` = '$current_item_id_from_loop' NO! Bad. There is NO debating, that's just wrong. The reason why it's bad is that queries should never, ever, ever be performed in a loop. It's a surefire way to kill site performance, as it costs more to hit a database repeatedly with queries than it does one long query (generally speaking). Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419825 Share on other sites More sharing options...
holly9 Posted March 20, 2013 Author Share Posted March 20, 2013 OP, explain better what you're trying to do, and post the table's structures. hi the tables are in the first post.the items were suppose to be in menus at the online store that shows how many items are in stock.there will also be menus of items in directory that shows the amount of active/approve ads.it's ok i'll just do another method instead. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419844 Share on other sites More sharing options...
Jessica Posted March 20, 2013 Share Posted March 20, 2013 ... The database tables. Not your generated HTML tables. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419846 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 ... ON Items.item = Ad.Members there is no column "members" and no table/alias "Ad" Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419848 Share on other sites More sharing options...
holly9 Posted March 20, 2013 Author Share Posted March 20, 2013 sorry it was a typo it was suppose to be: Members.item Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419856 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 to output a total you first need to select it SELECT Items.item, Members.total FROM Items LEFT OUTER JOIN ( SELECT item, COUNT( item ) as total FROM Members GROUP BY item )Members ON Items.item = Members.item Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419865 Share on other sites More sharing options...
monkeypaw201 Posted March 20, 2013 Share Posted March 20, 2013 The reason why it's bad is that queries should never, ever, ever be performed in a loop. It's a surefire way to kill site performance, as it costs more to hit a database repeatedly with queries than it does one long query (generally speaking). Thank you for elaborating to assist others (like myself) by learning something new. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419877 Share on other sites More sharing options...
holly9 Posted March 20, 2013 Author Share Posted March 20, 2013 to output a total you first need to select it SELECT Items.item, Members.total FROM Items LEFT OUTER JOIN ( SELECT item, COUNT( item ) as total FROM Members GROUP BY item )Members ON Items.item = Members.item Cool thank you!.Your code works.I had to add the total colomn first to the Members table. Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419930 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 I had to add the total colomn first to the Members table No, you didn't have to and you shouldn't. the total column comes from the count in the sub-query (alias Members) in that query Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1419933 Share on other sites More sharing options...
holly9 Posted March 21, 2013 Author Share Posted March 21, 2013 No, you didn't have to and you shouldn't. the total column comes from the count in the sub-query (alias Members) in that query Thanks!.I deleted the total coloumn in Members and it still works Link to comment https://forums.phpfreaks.com/topic/275890-is-it-possible-to-get-the-total-of-each-different-item-use-by-users-in-a-table-and-than-add-the-total-to-the-matching-item-in-another-table/#findComment-1420010 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.