I have a mysql query like...
SELECT OrderTransaction.buyer, OrderTransaction.parent_id
FROM order_transactions as OrderTransaction
INNER JOIN (
SELECT buyer
FROM order_transactions as dy
left join orders as ebay on ebay.id=dy.parent_id
where ebay.status='0' and dy.parent_id IN (
SELECT parent_id
FROM order_shipping_details as ds
left join orders as ebays on ebays.id=ds.parent_id
where ebays.status='0' and ebays.combined=0
GROUP BY ds.Street
HAVING count(ds.id) > 1
) and ebay.combined=0
group by dy.buyer
) dup ON dup.buyer=OrderTransaction.buyer
left join orders as ebay on ebay.id=OrderTransaction.parent_id
where ebay.market_type!='shopclue' and ebay.status='0' and ebay.combined=0
I need to optimize this query and want to remove the inner select with joins.
Any help would be appreciated. Thanks in advance.
Try this code below might be running faster than the one u currently using:
DROP TEMPORARY TABLE IF EXISTS temp1;
CREATE TEMPORARY TABLE temp1;
SELECT buyer
FROM order_transactions AS dy
LEFT JOIN orders AS ebay ON ebay.id=dy.parent_id
WHERE ebay.status='0'
AND dy.parent_id
IN (
SELECT parent_id
FROM order_shipping_details AS ds
left join orders AS ebays ON ebays.id=ds.parent_id
where ebays.status='0' and ebays.combined=0
GROUP BY ds.Street
HAVING count(ds.id) > 1)
AND ebay.combined= '0'
;
SELECT
OrderTransaction.buyer,
OrderTransaction.parent_id
FROM order_transactions AS OrderTransaction
INNER JOIN temp1 AS tmp ON tmp.buyer = OrderTransaction.buyer
LEFT JOIN orders AS ebay ON ebay.id = OrderTransaction.parent_id
WHERE ebay.market_type! = 'shopclub' AND ebay.status = '0' and ebay.combined = '0'
Please let me know if you have any questions!
Related
Please check the below code.
SELECT
`order`.idorder
, order_status_code.idorder_status_code
, order_status_code.order_status_code
, user.iduser
, `order`.required_delivery_date
, `order`.cancel
, `order`.date_created
, `order`.last_updated
, COUNT(order_item.idorder_item)
from
`order`
INNER JOIN order_status_code
ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user
ON `order`.iduser = user.iduser
INNER JOIN order_item
ON order_item.idorder = `order`.`idorder`
WHERE
`order`.iduser = 1
In here, I want the COUNT(order_item.idorder_item) to return the number of items under the idorder. In other words, if I run that SQL Part along, that would be like below
SELECT
COUNT(`idorder_item`)
from
order_item
where
idorder = 1
How can I get this done in my main query?
SELECT `order`.idorder,
order_status_code.idorder_status_code,
order_status_code.order_status_code,
user.iduser,
`order`.required_delivery_date,
`order`.cancel,
`order`.date_created,
`order`.last_updated,
COUNT(order_item.idorder_item),
(SELECT COUNT(`idorder_item`)
from order_item
where idorder=1) as count_idorder_item
from `order`
INNER JOIN order_status_code ON `order`.idorder_status_code = order_status_code.idorder_status_code
INNER JOIN user ON `order`.iduser = user.iduser
INNER JOIN order_item ON order_item.idorder = `order`.`idorder`
WHERE `order`.iduser= 1
my query is as follow:
SELECT * FROM `C_Institute`
WHERE `ID` IN
(SELECT `instituteID` FROM `C_Faculty` WHERE `ID`
IN (SELECT `facultyID` FROM `C_EducationalGroup` WHERE `ID`
IN (SELECT `educationalGroupID` FROM `C_StudyField` WHERE `ID`
IN (SELECT `studyFieldID` FROM `b_PersonEmployment` WHERE `personID`=1 ORDER BY `startDate` limit 1) )));
but mysql not support limit and in on subquery. I don't know how I can write this query.
can anyone help me please?
thanks in advance
You may rewrite this query a series of joins:
SELECT c1.*
FROM C_Institute c1
INNER JOIN C_Faculty c2
ON c1.ID = c2.instituteID
INNER JOIN C_EducationalGroup c3
ON c2.ID = c3.facultyID
INNER JOIN C_StudyField c4
ON c3.ID = c4.educationalGroupID
INNER JOIN b_PersonEmployment b
ON c4.ID = b.studyFieldID
WHERE b.personID = 1
ORDER BY b.startDate
LIMIT 1;
Assuming ID in each table is referring to the other table then,
Try this and let me know :
select * from C_Institute c join C_Faculty f on c.ID=F.instituteID left join C_EducationalGroup e on f.ID=e.facultyID left join C_StudyField sf on e.ID=sf.educationalGroupID left join b_PersonEmployment p on sf.ID=p.studyFieldID where p.personID=1 order by p.startDate limit 1;
Also, share your tables description for more clues
thanks for all your answer. but I think join is not good way to solve this problem. I've done it by two query(maybe not good idea but better than join):
SET #studyFieldID =(SELECT `studyFieldID` FROM `b_PersonEmployment` WHERE `personID`=1 ORDER BY `startDate` limit 1);
SELECT * FROM `C_Institute`
WHERE `ID` IN
(SELECT `instituteID` FROM `C_Faculty` WHERE `ID`
IN (SELECT `facultyID` FROM `C_EducationalGroup` WHERE `ID`
IN (SELECT `educationalGroupID` FROM `C_StudyField` WHERE `ID`=#studyFieldID
)));
I have a problem with my SQL join query. I have looked up other suggested answers and tried to apply it to my query, but it doesn't seem to be working.
I have this query:
SELECT SUM(p.quantity)
FROM stocktake_scans p
LEFT JOIN (
SELECT stocktake_area_id
FROM stocktake_areas
WHERE stocktake_id =8592 AND area_checked = 1
)d ON d.stocktake_area_id = p.stocktake_area_id
LEFT JOIN (
SELECT user_id
FROM stocktake_scan_edit
WHERE user_id =46521
)e ON e.user_id = p.stocktake_staff_id
WHERE p.stocktake_staff_id = 46521
And it gives me a result of 42, while I should get only 6. What is missing from the query?
I think you may have extra records with the same ID in your joined table that is where you are getting multiple rows returned from which is then calculating wrong in your sum, please try the below.
SELECT SUM(p.quantity) FROM stocktake_scans p LEFT JOIN ( SELECT distinct stocktake_area_id FROM stocktake_areas WHERE stocktake_id =8592 AND area_checked = 1 )d ON d.stocktake_area_id = p.stocktake_area_id LEFT JOIN ( SELECT distinct user_id FROM stocktake_scan_edit WHERE user_id =46521 )e ON e.user_id = p.stocktake_staff_id WHERE p.stocktake_staff_id = 46521
I've tried this query successfully with a limit. The following query runs endless without limit:
SELECT o.product_sku
FROM order_table o
WHERE EXISTS (SELECT 1
FROM (SELECT DISTINCT u.type_id,
u.charge_type,
u.billed_weight
FROM ups_table u
WHERE charge_type = 'order_shipping_table'
AND NOT billed_weight = '0'
) dtm
WHERE o.order_id = dtm.type_id)
GROUP BY product_sku
HAVING Count(product_sku) > 1
First of all you don't need these subqueries with DISTINCT and so on:
SELECT o.product_sku
FROM order_table o
WHERE EXISTS (SELECT 1
FROM ups_table u
WHERE charge_type = 'order_shipping_table'
AND NOT billed_weight = '0'
AND type_id=o.order_id
)
GROUP BY product_sku
HAVING Count(product_sku) > 1
I don't know your table structure (what do you want to count in the HAVING?) but you can try to change EXISTS to JOIN:
SELECT o.product_sku
FROM order_table o
JOIN ups_table u on (u.type_id=o.order_id)
AND (u.charge_type = 'order_shipping_table')
AND NOT (billed_weight = '0')
GROUP BY product_sku
HAVING Count(DISTINCT o.order_id) > 1
And you need indexes on o.order_id, o.product_sku, u.type_id,u.charge_type, u.billed_weight
I'm having an issue with the following query
select
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end as DateEnd, coup.coupon_id
from `order` ord
inner join order_product ordProduct on ord.order_id = ordProduct.order_id
inner join coupon_product coupProd on ordProduct.product_id = coupProd.product_id
inner join coupon coup on coupProd.coupon_id = coup.coupon_id
where (coup.date_end > curdate());
If I remvove the where clause, the query executes fine, otherwise it just hangs. Any ideas?
It's not a solution per se, but as a workaround, you could maybe get it done as a nested query. i.e. ,
SELECT * FROM (
SELECT
ord.order_id,
ordProduct.product_id,
coupProd.product_id,
coup.date_end AS DateEnd, coup.coupon_id
FROM `order` ord
INNER JOIN order_product ordProduct ON ord.order_id = ordProduct.order_id
INNER JOIN coupon_product coupProd ON ordProduct.product_id = coupProd.product_id
INNER JOIN coupon coup ON coupProd.coupon_id = coup.coupon_id)
WHERE (DateEnd > CURDATE());