I have this SQL query:
select sum(qty) as 'total' from `sales_flat_quote_item`
where item_id not in (
select item_id from `sales_flat_quote_item_option`
where item_id in (
select item_id from `sales_flat_quote_item`
where quote_id = 12670
)
and code = 'is_gift'
)
and quote_id = 12670 and parent_item_id is null
My task is make this query using JOIN clause. Well query itself is quite weird. sales_flat_quote_item is used in both query and subquery. This cause some efficient issues.
I'm not SQL expert, so I'm asking you. I can handle one lvl of nested select queries on my own, but I don't know what to do in this situation.
We use Mysql db engine v. 5.1
I was able to make it like this:
select A.qty, A.item_id from `sales_flat_quote_item` as A
where A.item_id not in
(
select B.item_id from
`sales_flat_quote_item_option` as B
inner join `sales_flat_quote_item` as C
on C.item_id = B.item_id
where B.code = 'is_gift' and
C.quote_id = 834
)
and A.quote_id = 834 and A.parent_item_id is null
Is it possible to make it without using any sub-queries?