Can anyone see what is wrong with the below query?
The Value I have is the products_model number 000011195001
What I need is the sum attributes_stock of all corresponding products_id in Table products_attribute
The sum of all attributes_stock of products_id 1726 in Table products_attribute is 500
These 500 need I in Table products in Field products_quantity
When I run it I get:
UPDATE products As C INNER JOIN (
SELECT SUM( attributes_stock ) AS products_quantiry
FROM products_attributes
WHERE products_id IN(
SELECT products.products_id
FROM products
WHERE products_model LIKE '000011195001'
)
)
AS A ON products.products_id = products_attributes.products_id
SET C.products_quantity = A.products_quantiry
Table products
products_model, products_id,products_quantity
values
000011195001, 1726
Table products_attribute
products_id,attributes_stock
1726, 300
1726, 150
1726, 50
Thanks in advance
here (i have done a sql fiddle but the website isn't working right now :'():
UPDATE products
INNER JOIN
(SELECT products_id, SUM(attributes_stock) as qty
FROM products_attribute
GROUP BY products_id) T ON T.products_id = products.products_id
SET products.products_quantity = T.qty
WHERE products_model = '000011195001'
;
Related
I've two tables
magento_customerbalance -mcb
column -
1. balance_id
2. customer_id
3. website_id
4. amount
5. base_currency_code
customer_entity_varchar -cev
column -
1. value
2. attribute_id
3. entity_id
4. value
I've tried to find customer_id from mcb which does not have cev.attribute_id 5 with the following SQL queries
Query #1:
SELECT COUNT(mcb.customer_id)
FROM magento_customerbalance mcb;
Results :
total customer from mcb =121
Query #2:
SELECT mc.customer_id
FROM magento_customerbalance mc
INNER JOIN customer_entity_varchar cev ON cev.entity_id = mc.customer_id
WHERE cev.attribute_id = 5;
Results :
total customers from mcv with attribute id 5 = 100
121 -100 =21
customer who does not have attribute id 5 = 21
How we can get these 21 mcb.customer_id records with a SQL query?
In cev table there are multiple attribute ids for same customer.
Use the not exists operator with a correlated subquery as the following:
SELECT mcb.customer_id /* add other columns as needed */
FROM magento_customerbalance mcb
WHERE NOT EXISTS
(
SELECT 1 FROM customer_entity_varchar cev
WHERE cev.entity_id = mcb.customer_id AND
cev.attribute_id = 5
)
And if you want only customer_ids which have an entity_id in the cev table add this condition:
AND EXISTS
(
SELECT 1 FROM customer_entity_varchar cev
WHERE cev.entity_id = mcb.customer_id
)
See demo
Did you tried not equal to 5, !=5 ?
SELECT mc.customer_id FROM magento_customerbalance mc inner join customer_entity_varchar
cev on cev.entity_id = mc.customer_id
where cev.attribute_id != 5 OR cev.attributee IS NULL;
im having issues getting a query to output how i want,
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
`orders`.`quantity`
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159
AND `orders`.`order_status` = 'NOTED'
which is returning the results:
item_id item_code item_name quantity
1271 RA001G Green Mop Bucket 12L 2
1270 RA001 Blue Mop Bucket 12L 1
1270 RA001 Blue Mop Bucket 12L 1
but i would like it to bring back distinct item_id with the quantity added together how ever when i've tried to add distinct and count i end up only have one line returned.
If you want to sum up the quantities for same items, try grouping over item_id. Like this:
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
sum(`orders`.`quantity`) as quantity,
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159
AND `orders`.`order_status` = 'NOTED'
GROUP BY `orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`
Please use group by clause
SELECT
`orders`.`item_id`,
`products`.`item_code`,
`products`.`item_name`,
sum(`orders`.`quantity`) as quantity
FROM
`orders`
JOIN `products` ON `orders`.`item_id` = `products`.`id`
JOIN `suppliers` ON `products`.`supplier_ref` = `suppliers`.`supplier_ref`
WHERE
`suppliers`.`id` = 159 AND `orders`.`order_status` = 'NOTED'
group by
`orders`.`item_id`;
Is it possible to do a select and join a table, then update that table?
I'm basically looking to increase the price by 5% on all products owned by a particular supplier. The problem is the pricing details are held in a separate table to the products themselves.
I know this syntax is not correct, but it will give you the gist of what i'm trying to achieve:
update
products_quantity_pricing set price = price + (price/100*5)
where (select
products.supplier_id,
products_quantity_pricing.price
from products
join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
You put the JOIN directly into the UPDATE query:
UPDATE products_quantity_pricing AS pqp
JOIN products AS p ON pqp.product_id = p.id
SET price = price + (price/100*5)
WHERE p.supplier_id = 7
update products_quantity_pricing set price = price + (price/100*5)
where products_quantity_pricing.price in
(select products_quantity_pricing.price
from products join
products_quantity_pricing on products_quantity_pricing.product_id = products.id
where products.supplier_id = 7 )
There are three tables:
shop_product_skus - a table for the item 1 product ( 1 to many )
shop_product_skus(id,product_id,sku,sort,name,image_id,price,primary_price,purchase_price,compare_price,count,available,dimension_id,file_name,file_size,file_description,virtual)
shop_product_stocks - a table of how many products in different warehouses
shop_product_stocks(sku_id, stock_id, product_id, count)
shop_product_ostatki - this is a temporary table with the amount of product , price and sku from one warehouse , for example stock_id = 1 ( 1 is updated every 30 minutes)
shop_product_ostatki (id, sku, count, price)
Foreign Key Relations
shop_product_skus.product_id = shop_product_stocks.product_id
shop_product_skus.sku = shop_product_ostatki.sku
shop_product_skus.id = shop_product_stocks.sku_id
Now I need to update data from shop_product_ostatki
In table shop_product_skus
update shop_product_skus.price = shop_product_ostatki.price * 1.07,
shop_product_skus.purchase_price = shop_product_ostatki.price * 0.7
where shop_product_skus.sku = shop_product_ostatki.sku
In table shop_product_stocks
update if there is no prompt to add
shop_product_stocks.count = shop_product_ostatki.count,
shop_product_stocks.price = shop_product_ostatki.price * 1.07
where shop_product_skus.sku = shop_product_ostatki.sku
and shop_product_skus.id = shop_product_stocks.sku_id
Probably explained a little confusing , I do it all in one query.
For 4 days I struggle with this issue, search by stackoverflow enjoyed similar questions not found :(
ps sorry for my english
UPDATE
http://sqlfiddle.com/#!2/90f67
UPDATE
I'm try this, it's not worked
INSERT INTO shop_product_stocks (sku_id ,stock_id ,product_id ,'count')
(SELECT id.b ,product_id.b ,sku.b ,sort.b ,name.b ,image_id.b, price.b ,primary_price.b ,purchase_price.b ,available.b FROM shop_product_skus b
LEFT JOIN shop_product_ostatki c ON c.sku = b.sku
LEFT JOIN shop_product_skus a ON a.sku = c.sku
WHERE shop_product_stocks.stock_id = 1
)
ON DUPLICATE KEY UPDATE
'count' = 'count'.c
UPDATE
error in select
SELECT * FROM shop_product_skus AS b
LEFT JOIN shop_product_ostatki_sander AS c ON c.sku = b.sku
LEFT JOIN shop_product_stocks AS a ON a.sku_id = b.id
WHERE a.stock_id = 1
but all expr not worked
I have two tables:
customer with schema_id
Schema table has: schema_id, period, amt, updated_date
I need to take join of customer and schema but only retrieve the latest record joined and not the others.
customer table
cust_id name schema_id
1 ABC 1
Schema table
schema_id period amt updated_date
1 1 100 2010-4-1
1 2 150 2011-4-1
If you need the max(updated_date) for each schema_id, then you can use an subquery:
select c.cust_id, c.name, c.schema_id, s.period, s.amt, s.updated_date
from customer c
inner join
(
select s1.schema_id, s1.period, s1.amt, s1.updated_date
from `schemas` s1
inner join
(
select schema_id, max(updated_date) MaxDate
from `schemas`
group by schema_id
) s2
on s1.schema_id = s2.schema_id
and s1.updated_date = s2.maxdate
) s
on c.schema_id = s.schema_id
See SQL Fiddle with Demo
The subquery is then used in a join back to your table to return the rows that have the matching date and schema_id.
If I understood your problem, you need to take lastest register of the "schema".
I think you need to use max() function. So, try the query below:
select *
from customer c,
schema s
where c.schema_id = s.schema_id
and s.updated_date = ( select max(s2.updated_date)
from schema s2
where s2.schema_id = s.schema_id
)
Regards!
Edmilton