2 COUNT queries multiplying results - php

I am trying to execute two COUNT statements across 3 joins. The first Count shows the correct number but the second one seems to multiply the counts together for some reason? I checked the link which was marked as duplicate but that example doesn't have any JOINS in it.
SELECT
COUNT(DISTINCT `outlet_id`) AS `outlets`,
`prod_name`,
COUNT(`purchased`) AS `vouchersleft`
FROM
`prod_outlets` AS `po`
INNER JOIN `bb_products` AS `bbp` ON po.`product_id` = bbp.`prod_id`
INNER JOIN `vouchers` AS `v` ON v.`product_id` = bbp.`prod_id`
GROUP BY
bbp.`prod_id`;
What it should display is 3 branches and 5 vouchers. But it is outputting 3 branches and 15 vouchers. So, the second COUNT is multiplying by the first i.e.: 3 x 5 = 15

From the description what i understood is you are getting cross product that is why you are getting wrong number for vouchersleft, what i suggest you to calculate your count in a sun clause and then join this clause with your main query like
SELECT
COUNT(DISTINCT `outlet_id`) AS `outlets`,
`prod_name`,
v.vouchersleft
FROM
`prod_outlets` AS `po`
INNER JOIN `bb_products` AS `bbp` ON po.`product_id` = bbp.`prod_id`
INNER JOIN (
SELECT product_id, COUNT(*) vouchersleft
FROM vouchers
GROUP BY product_id
) AS `v` ON v.`product_id` = bbp.`prod_id`
GROUP BY
bbp.`prod_id`;

"SELECT COUNT(DISTINCT `outlet_id`) as `outlets`,
`prod_name`,
COUNT(distinct `purchased`) as `vouchersleft`
FROM `prod_outlets` as `po`
INNER JOIN `bb_products` as `bbp`
ON po.`product_id` = bbp.`prod_id`
INNER JOIN `vouchers` as `v`
ON v.`product_id` = bbp.`prod_id`
GROUP BY bbp.`prod_id`";

Related

MYSQL use outer select value in inner select

I know that this question asked frequently but i didn't find any answer (or that its not possible):
I'v got this query
SELECT
`items`.`id`,
`items`.`part_number`,
`item_categories`.`name` AS category,
`suppliers`.`name` AS supplier,
`items`.`supplier_id`,
`items`.`name`,
`items`.`inventory`,
`items`.`package_items`,
`items`.`order_step`,
`items`.`price`,
`items`.`discount`,
`items`.`scale`,
`items`.`by_scale`,
`items`.`has_tax`,
`items`.`category_id`,
`items`.`enable`,
orders.last_orders_amount
FROM
`items`
INNER JOIN
`suppliers` ON `items`.`supplier_id` = `suppliers`.`id`
INNER JOIN
`item_categories` ON `items`.`category_id` = `item_categories`.`id`
INNER JOIN (
SELECT
GROUP_CONCAT(
JSON_EXTRACT(
`orders`.`items`,
CONCAT('$."',
"3",
'".amount')
)
ORDER BY
`orders`.`createDate` DESC
) AS last_orders_amount
FROM
`orders`
WHERE
JSON_EXTRACT(`orders`.`items`,
'$."3"') IS NOT NULL
LIMIT 4
) orders ON orders.last_orders_amount IS NOT NULL
WHERE
1
basically i want to get all the 'items' with there last 4 occurrences in 'orders' base on item.id.
I need to replace the number 3 in that query to item.id from the outer join
(i know that there is INNER JOIN cause to get only items that have occurrences in orders)

Mysql left join issue with 3 tables not returning all rows from first table

I have below query
select catid, cat_name, currency, count(is_reporting_category_sales.id) as total_sales,
sum(total_sales) as total_earning
from is_category
left join is_reporting_category_sales on is_category.catid = is_reporting_category_sales.category_id
join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id
group by catid, cat_name, currency
ORDER BY `is_category`.`cat_name` ASC
but this is returning only rows that are common in is_category and is_reporting_category_sales, is_reporting_order but I want to fetch all rows from is_category table. And if there is no order for the category then 0 as total_earning and total_sales.
You have to Use Left Join
left join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id
Instead of
join is_reporting_order on is_reporting_order.id = is_reporting_category_sales.order_id
Perhaps using left outer joins you might get the results you expect ( had to guess at some of the aliases for columns btw so some of them might be wrong )
select c.`catid`, c.`cat_name`, `currency`, count(i.`id`) as 'total_sales', sum(`total_sales`) as 'total_earning'
from `is_category` c
left outer join `is_reporting_category_sales` i on c.`catid` = i.`category_id`
left outer join `is_reporting_order` on o.`id` = i.`order_id`
group by c.`catid`, c.`cat_name`, `currency`
order by c.`cat_name` asc;

SUM() not working in MySQL : SUM() with DISTINCT

I have 4 tables called shops, users, review and rating.
I want to get all reviews for the corresponding shop with reviewed user details and also overall rating for that shop.
I have done almost with the single query. But the problem is if the shop has same rating for multiple times by same user its consider as single rating. But that rating count was correct.
i.e
from this table user_id 3 was rated shop_id 1 as 4 times. So the count is 4 and total_rating is 17.
My query is
select review.comments, users.username, count(distinct rating.id) as rating_count,
sum(distinct rating.rating) as total_rating from users
left join review on users.id = review.user_id and review.shop_id='1'
left join rating on users.id = rating.user_id and rating.shop_id='1'
where review.shop_id='1' or rating.shop_id='1'
group by users.id, review.user_id, rating.user_id, review.id
When I run this query I got
But I need total_rating 17 for user_id 3..
Check this fiddle
You put DISTINCT IN sum( rating.rating) as total_rating, thats why the result(12=17-5), since it will include 5 only once while computing sum.
select review.comments, review.user_id, count(distinct rating.id) as rating_count,
sum( rating.rating) as total_rating from users
left join review on users.id = review.user_id and review.shop_id='1'
left join rating on users.id = rating.user_id and rating.shop_id='1'
where review.shop_id='1' or rating.shop_id='1'
group by users.id, review.user_id, rating.user_id, review.id
Here is SQLFiddle
Sample Output :
Hope this helps
Try this - Remove the distinct from sum(rating.rating). Since you gave sum(distinct rating.rating), it is ignoring one 5 that user 3 gave to store 1.
select review.comments, users.username, count(distinct rating.id) as rating_count,
sum(rating.rating) as total_rating from users
left join review on users.id = review.user_id and review.shop_id='1'
left join rating on users.id = rating.user_id and rating.shop_id='1'
where review.shop_id='1' or rating.shop_id='1'
group by users.id, review.user_id, rating.user_id, review.id
First of all: It makes no sense to outer-join records from a table and then remove them in the WHERE clause. With left join review ... you say: find a matching record in table review, and if you don't find any, then add nulls, so we keep the users record. Then with where review.shop_id='1' you say: keep only records where you actually found a record in review. So you are dismissing the records that you just took the pain to keep. Your WHERE clause renders your LEFT OUTER JOINS mere INNER JOINS.
As to your actual problem: That stems from joining all tables first and only then trying to get aggregates from the resulting records. Aggregate before joining instead:
select
rev.comments,
usr.username,
coalesce(rat.rating_count, 0) as rating_count,
rat.total_rating
from review rev
join users usr on users.id = review.user_id
left join
(
select user_id, shop_id, count(*) as rating_count, sum(rating) as total_rating
from rating
group by user_id, shop_id
) rat on rat.user_id = usr.id and rat.shop_id = rev.shop_id
where rev.shop_id = 1
group by rev.id;

join other tables

SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage
from user_pr where user_id=49
i also want to join
select users.firstname from users where type=1
how can i combine both in one query
Take the two table names, put JOIN between then and ON and join condition after the second one.
SELECT saleamount/(SELECT SUM(saleamount)
from user_pr where user_id=49) *100 as totalPercentage,
users.firstname
from user_pr JOIN users ON user_pr.user_id = users.user_id
where user_pr.user_id = 49
and users.type = 1

Counting rows from table after joins

I have a query that selects data from 4 tables via joins, i want to also count the number rows in a fifth table containing a matching foreign key.
This is what my current query looks look like, and it doesnt work
"SELECT
ph.pheed_id,ph.user_id,ph.datetime,ph.repheeds,
ph.pheed,fav.id,fav.P_id,fav.datetime as stamp,
u.username,ava.avatar_small
COUNT(pheed_comments.comment_id) as comments
FROM favourite_pheeds fav
INNER JOIN pheeds ph ON ph.pheed_id=fav.P_id
INNER JOIN users u ON u.id=ph.user_id
INNER JOIN profiles pr ON pr.user_id=ph.user_id
LEFT JOIN user_avatars ava ON ava.avatar_id=pr.avatar
ORDER BY stamp DESC
LIMIT $offset,$limit";
How do i count the number rows in a fifth table containing a matching foreign key.
select ph.pheed_id,
ph.user_id,
ph.datetime,
ph.repheeds,
ph.pheed,
fav.id,
fav.P_id,
fav.datetime as stamp,
u.username,
ava.avatar_small,
coalesce(pcc.Count, 0) as comments_count
from favourite_pheeds fav
inner join pheeds ph on ph.pheed_id = fav.P_id
inner join users u on u.id = ph.user_id
inner join profiles pr on pr.user_id = ph.user_id
left join user_avatars ava on ava.avatar_id = pr.avatar
left outer join (
select pheed_id, count(*) as Count
from pheed_comments
group by pheed_id --took a guess at the column name here
) pcc on ph.pheed_id = pcc.pheed_id
order by stamp desc
LIMIT $offset, $limit

Categories