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)
Related
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`";
Sum of a multiplication of two columns grouped by another column in an inner join of three tables returns wrong value.
Below are my three tables:
Table1:
Table2:
Table3:
My Query is as below:
SELECT c.price, c.quantity, SUM( c.quantity * c.price ) AS price,
group_concat( a.rate
SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
FROM tax_wa a
INNER JOIN tax_rate_class b ON a.tax_rate_id = b.tax_rate_id
INNER JOIN inv_item c ON b.tax_class_id = c.tax_class_id
WHERE c.invoice_id = '17'
GROUP BY c.hsn
And the result is:
But above one is not correct... To expain it, if you run the below query on the inv_item table (alone, with no joins) you get correct results:
SELECT price, quantity, sum( quantity * price )
FROM `inv_item`
WHERE invoice_id = '17'
GROUP BY hsn
Result is good:
Above result the wrong value calculated
if you add all
Presumably, you want the sum():
SELECT SUM(c.price), SUM(c.quantity), SUM( c.quantity * c.price ) AS price,
group_concat( a.rate SEPARATOR '<br>' ) AS rates, c.hsn AS hsn
FROM tax_wa a INNER JOIN
tax_rate_class b
ON a.tax_rate_id = b.tax_rate_id INNER JOIN
inv_item c
ON b.tax_class_id = c.tax_class_id
WHERE c.invoice_id = 17
GROUP BY c.hsn;
Okay so I have three(3) tables that i want to join together
tableA is the main details and primary key is row_id autoincremented
tableB is the exteded details and primary/foreign key is row_id coming from tableA
tableC stores unordered ratings and comments for a particular row_id
I want to join all these tables so that I can see all details plus the number of instances in tableC for a row_id and the avg rating.
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT COUNT( 1 ) AS 'count', Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC
The result of this query combines all properly but the same count and avg is displayed in all rows.
Looks like you want to group the records by row_id in inner query. In which case, you need to SELECT row_id instead of COUNT(1), try this:
SELECT *
FROM `tableA` A
LEFT JOIN `tableB` B
ON A.`row_id` = B.`row_id`
LEFT JOIN (
SELECT row_id, Avg(`row_rating`) AS 'avg'
FROM `tableC`
GROUP BY tableC.`row_id`
)C
ON C.`row_id` = A.`row_id`
ORDER BY C.`avg` ASC
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;
Not sure if the title explains the situation right but I will try to do my best explaining here.
I have a table with 3 fields linked to other tables and I want to get all the Rows grouped in the following way:
item_id, user_id, group_id
1 2 3
2 2 3
3 4 5
4 2 4
In my query i want in comma separated format all the items_id grouped by group_id i also have some extra conditions on the WHERE clause that's why the inner join
That i can do like with this query
"SELECT
GROUP_CONCAT( DISTINCT A.item_id ) AS ids
FROM tableA A
INNER JOIN tableB B ON(tableA.id = tableB.id)
WHERE xxxxx
GROUP BY A.group_id
"
Later i can loop the results and using the comma separated to inner loop every id within the result
But i also want to group it by user_id in order to do something like this
foreach( query_results.... ){
foreach( group_id.... ){
foreach( item_id.... ){
// Display info
}
}
}
Any ideas on this?
using subqueries, we can get both itemids, userids as two seperate columns
select T1.group_id, T1.itemids, T2.userids
FROM
(SELECT group_id,
GROUP_CONCAT( DISTINCT A.item_id ) AS itemids
FROM table1 A
group by group_id) T1
INNER JOIN
(
SELECT
group_id, GROUP_CONCAT( DISTINCT A.user_id ) AS userids
FROM table1 A
group by group_id
) T2
on T1.group_id = T2.group_id
Use 2 GROUP_CONCATS
SELECT GROUP_CONCAT( DISTINCT A.item_id ) AS ids,
GROUP_CONCAT( DISTINCT A.group_id ) AS groups
FROM tableA A
INNER JOIN tableB B ON(tableA.id = tableB.id)
WHERE xxxxx
GROUP BY A.group_id
Loop your resource then use Explode on the groups and ids value and loop them both as you want