Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
PHP MySQL Count rows from INNER JOIN - GROUP BY
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 389773" data-attributes="member: 71840"><p>What I'm trying to do here, is I want to output the count of the `id` and `user_id` columns from `items`, by users which have the rank `1`.</p><p>Let's say there's 8 rows in `items` which have a `base_item` of <strong>400</strong>, and these 8 rows, where 2 rows of them has a `user_id` of <strong>4</strong> and the existing left has a `user_id` of <strong>6</strong></p><p>The output should then be:</p><p>`<em>$countItems = 8</em>` and `<em>$countUsers = 2</em>`.</p><p>[PHP]</p><p> $stmt = $conn->pdo->prepare("</p><p> SELECT COUNT(*),`user_id` FROM `items`</p><p> INNER JOIN `users`</p><p> ON `users`.`id` = `items`.`user_id`</p><p> WHERE `base_item` = :i AND `rank` = 1</p><p> GROUP BY `user_id`,`items`.`id`</p><p> ");</p><p> $stmt->bindParam(':i', '400', $db->PARAM_INT); // item_id in `values` table</p><p> $stmt->execute();</p><p> if($inv = $stmt->fetch($db->FETCH_ASSOC)) {</p><p> $countItems = $inv['id'];</p><p> $countUsers = $inv['user_id'];</p><p> }</p><p>[/PHP]</p><p>I have tried several methods, but I still keep getting output `2` and `2` even though the results should be `8` and `2`.</p><p></p><p><strong>Table Info for `values`:</strong></p><p><img src="https://i.stack.imgur.com/YaXn4.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p><strong>Table Info for `items`:</strong></p><p><img src="https://i.stack.imgur.com/Ez2X6.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p>Tried Stackoverflow, but their users are a pain in the ass.</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 389773, member: 71840"] What I'm trying to do here, is I want to output the count of the `id` and `user_id` columns from `items`, by users which have the rank `1`. Let's say there's 8 rows in `items` which have a `base_item` of [B]400[/B], and these 8 rows, where 2 rows of them has a `user_id` of [B]4[/B] and the existing left has a `user_id` of [B]6[/B] The output should then be: `[I]$countItems = 8[/I]` and `[I]$countUsers = 2[/I]`. [PHP] $stmt = $conn->pdo->prepare(" SELECT COUNT(*),`user_id` FROM `items` INNER JOIN `users` ON `users`.`id` = `items`.`user_id` WHERE `base_item` = :i AND `rank` = 1 GROUP BY `user_id`,`items`.`id` "); $stmt->bindParam(':i', '400', $db->PARAM_INT); // item_id in `values` table $stmt->execute(); if($inv = $stmt->fetch($db->FETCH_ASSOC)) { $countItems = $inv['id']; $countUsers = $inv['user_id']; } [/PHP] I have tried several methods, but I still keep getting output `2` and `2` even though the results should be `8` and `2`. [B]Table Info for `values`:[/B] [IMG]https://i.stack.imgur.com/YaXn4.png[/IMG] [B]Table Info for `items`:[/B] [IMG]https://i.stack.imgur.com/Ez2X6.png[/IMG] Tried Stackoverflow, but their users are a pain in the ass. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
PHP MySQL Count rows from INNER JOIN - GROUP BY
Top