magento2 mysql update table query - mysql

If someone can help with this MySQL query that would be great
I want to update this table
UPDATE catalog_product_entity_int
This query brings an array of values and works.
select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id;
This is what I want to set based on the above query
SET catalog_product_entity_int.value = 1
where catalog_product_entity_int.attribute_id ='579'
and catalog_product_relation.parent_id = catalog_product_entity_int.entity_id
I'm not sure if I need to get the select query into a temp table first and if so how?

Theeasiest way is to join both table
i added aliases for he table to make it more readable
UPDATE catalog_product_entity_int t1,(select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id) t2
SET t1.value = 1
where t1.attribute_id ='579'
and t2.parent_id = t1.entity_id
catalog_product_entity.sku and catalog_product_flat_1.special_price aren't used in the update and so you should check if they are needed
Try the select query
SELECT t1.value, t1.entity_id
FROM
catalog_product_entity_int t1 INNER JOIN (select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
GROUP BY catalog_product_relation.parent_id) t2
ON t2.parent_id = t1.entity_id
where t1.attribute_id ='579'

Thank you for the help. In the end this is what i did to make it work.
First create a temp table to store the values of the query
CREATE TABLE on_sale_updating AS
select catalog_product_entity.sku,catalog_product_relation.parent_id,catalog_product_flat_1.special_price
from catalog_product_entity
left join catalog_product_relation on (catalog_product_entity.entity_id = catalog_product_relation.parent_id)
left join catalog_product_flat_1 on (catalog_product_flat_1.entity_id = catalog_product_relation.child_id)
where catalog_product_entity.type_id = "configurable"
and catalog_product_flat_1.special_price > 1
and catalog_product_flat_1.special_to_date IS NULL OR catalog_product_flat_1.special_to_date >= CURDATE()
GROUP BY catalog_product_relation.parent_id;
Then run an update query against the temp table
update catalog_product_entity_int
inner join on_sale_updating ON catalog_product_entity_int.entity_id = on_sale_updating.parent_id
SET catalog_product_entity_int.value = 1
where catalog_product_entity_int.attribute_id ='579'
Once run for the first time add
DROP TABLE on_sale_updating;
to the first part.

Related

MySQL Multiple Aliased Joins Against Joined Table

I have this query, which works fine:
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
So, it's a query on table 1 with a join on table 2, then subsequent joins from multiple aliased joins of table 3 on table 2, but as soon as I add further joins, I get no results and I'm not sure why, for example:
select table_1.*, coalesce(test_1.type, test_2.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and `table_2`.`column` = 'L'
inner join `table_3` as `test_2` on `test_2`.`code` = `table_2`.`column` and `table_2`.`column` = 'H'
Can anyone explain what I have done wrong?
Try LEFT join on table_3 . If there are no records for table_3, that's why you yield no results, due to the INNER join.
And actually, you're not joining any columns on table_3. Is most likely the issue.
What is the expected sample result of the second of your query ?
Could you please try this query ?
select table_1.*, coalesce(test_1.type) as type
from `tbl_1`
left join `table_2` on `table_1`.`table_1_id` = `table_1`.`id`
inner join `table_3` as `test_1` on `test_1`.`code` = `table_2`.`column` and
(`table_2`.`column` = 'L' or `table_2`.`column` = 'H')

Update with Subquery non relational table

I have the follow sql query but when I execute It gives me a message error: Unknown column 't1.ip' in 'where clause'.
If I hard code t1.ip in subquery just for testing, It works perfectly.
UPDATE
report_a t1,
(SELECT
location.country, region.name, location.city
FROM
geoip
INNER JOIN
location
ON
geoip.locId = location.locId
INNER JOIN
region
ON
region.country = location.country
AND
region.region = location.region
WHERE
INET_ATON(t1.ip) BETWEEN startIpNum AND endIpNum
LIMIT 1) AS t2
SET
t1.country = t2.country,
t1.city = t2.city,
t1.state = t2.name;
Someone have any idea how i can do this query?
Thanks
You are looking for a join. I am a bit unclear on what the LIMIT 1 is supposed to be doing. I moved that into the outer query, although that might not be correct:
UPDATE report_a t1,
(SELECT location.country, region.name, location.city, startIpNum, endIpNum
FROM geoip INNER JOIN
location
ON geoip.locId = location.locId INNER JOIN
region
ON region.country = location.country AND
region.region = location.region
) glr
ON INET_ATON(t1.ip) BETWEEN glr.startIpNum AND glr.endIpNum
SET t1.country = t2.country,
t1.city = t2.city,
t1.state = t2.name
LIMIT 1;
As I think about it, you probably do not want the LIMIT 1. Why would there be multiple matches to the subquery?

mySQL Update on having count grouped by

I've got the following query:
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching, COUNT(t3.part) AS AantalOnderdelen
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0 #and t2.part_detailtype_id=825
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
Now, this results in 3000 rows which have t1.exact_matching 0. See below for the output of the select query (just 1 row)
shop_id;cat_id;detailtype_id;exact_matching
6;11111;14519;0
Now I'd like to update the exact_matching column to 1 for all the rows returned by the select query from above. I can't figure out how to write the update statement though, as I want to make sure only these 3000 rows are affected which meet the having and where conditions from the above select query.
I got this now:
UPDATE tbldetailspershop SET exact_matching = 1
WHERE (shop_id, cat_id, detailtype_id) IN
(
SELECT t1.shop_id, t1.cat_id, t1.detailtype_id
FROM tbldetailspershop t1
LEFT JOIN tbltranslationscompound t2 ON t1.compound_group_id = t2.group_id AND t2.part_detailtype_id=825
LEFT JOIN tbltranslationscompound t3 ON t1.compound_group_id = t3.group_id
WHERE t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
GROUP BY t1.shop_id, t1.cat_id, t1.detailtype_id
HAVING COUNT(t3.part)=1
)
update shopdetails
set exact_matching = 1
where (shop_id, cat_id, detailtype_id, exact_matching) in
(
select t1.shop_id, t1.cat_id, t1.detailtype_id, t1.exact_matching from shopdetails t1
left join transcomp t2 on t1.compound_group_id = t2.group_id and t2.part_detailtype_id=825
left join transcompt3 on t1.compound_group_id = t3.group_id
where t1.detailtype_id=14519 AND t1.cat_id=11111 AND t1.exact_matching=0
group by t1.shop_id, t1.cat_id, t1.detailtype_id
having count(t3.part)=1
)

updating MySQL table - You can't specify target table for update in From clause

I have the following query:
SELECT entity_id AS product_id FROM catalog_product_entity cpe
LEFT JOIN cataloginventory_stock_item csi ON csi.product_id = cpe.entity_id
WHERE cpe.type_id = 'configurable'
AND csi.is_in_stock = 0
AND (SELECT SUM(qty) FROM catalog_product_relation cpr LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpr.child_id WHERE cpr.parent_id = cpe.entity_id) > 0
Which returns the following results:
product_id
-----------
912
906
894
559
364
I am trying to update the is_in_stock column within the cataloginventory_stock_item table, based off of the product_id of the table existing in the results of the above query.
What I tried to do was this:
UPDATE cataloginventory_stock_item
SET is_in_stock = 1
WHERE product_id IN (
SELECT entity_id AS product_id FROM catalog_product_entity cpe
LEFT JOIN cataloginventory_stock_item csi ON csi.product_id = cpe.entity_id
WHERE cpe.type_id = 'configurable'
AND csi.is_in_stock = 0
AND (SELECT SUM(qty) FROM catalog_product_relation cpr LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpr.child_id WHERE cpr.parent_id = cpe.entity_id) > 0
)
And I receive the following error:
You can't specify target table 'cataloginventory_stock_item' for update in FROM clause
I can't seem to figure out how to restructure the query to work. Any help is greatly appreciated.
I believe the following multi-table UPDATE does the same thing, without using subqueries:
UPDATE cataloginventory_stock_item AS csi
JOIN catalog_product_entity AS cpe ON csi.product_id = cpe.entity_id
JOIN catalog_product_relation AS cpr ON cpr.parent_id = cpe.entity_id
JOIN cataloginventory_stock_item AS cisi ON cisi.product_id = cpr.child_id
SET csi.is_in_stock = 1
WHERE cpe.type_id = 'configurable' AND csi.is_in_stock = 0 AND cisi.qty > 0;
I'm assuming that cisi.qty is never negative. So if any row with qty>0 is found, then SUM(qty) will be greater than zero.
PS: Your original query uses LEFT JOIN as though it's an INNER JOIN. You should study how the different types of joins work.
Try this one by giving the alias to you select query
UPDATE cataloginventory_stock_item
SET is_in_stock = 1
WHERE product_id IN (
SELECT new_table.product_id FROM (
SELECT entity_id AS product_id FROM catalog_product_entity cpe
LEFT JOIN cataloginventory_stock_item csi ON csi.product_id = cpe.entity_id
WHERE cpe.type_id = 'configurable'
AND csi.is_in_stock = 0
AND (SELECT SUM(qty) FROM catalog_product_relation cpr LEFT JOIN cataloginventory_stock_item cisi ON cisi.product_id = cpr.child_id WHERE cpr.parent_id = cpe.entity_id) > 0
) new_table
)

Mysql update using values from select

I have two queries. The first will return multiple rows:
SELECT parent_entry_id,child_entry_id FROM exp_playa_relationships WHERE parent_field_id = '34';
...And I would like to use the values (parent_entry_id,child_entry_id) and incorporate them into this query, replacing 'x' and 'y', and do it for each row returned by the first query.
UPDATE exp_channel_data AS t1,
(
SELECT field_id_46,field_id_47 FROM exp_channel_data WHERE entry_id = 'x') AS t2
SET t1.field_id_60 = t2.field_id_46, t1.field_id_61 = t2.field_id_47
WHERE t1.entry_id = 'y';
I think I need to use another JOIN, but I can't figure out how to implement one in my example. Any help would be much appreciated.
I think this is what you're after:
UPDATE exp_playa_relationships AS t0
JOIN exp_channel_data AS t1
ON t1.entry_id = t0.child_entry_id
JOIN exp_channel_data AS t2
ON t2.entry_id = t0.parent_entry_id
SET t1.field_id_60 = t2.field_id_46
, t1.field_id_61 = t2.field_id_47
Try this query
UPDATE exp_channel_data a1 INNER JOIN exp_playa_relationships a ON a1.entry_id = a.child_entry_id
INNER JOIN exp_channel_data b ON a.parent_entry_id = b.entri_id
SET a1.field_id_60 = b.field_id_46, ta1.field_id_61 = b.field_id_47
WHERE parent_field_id = '34'
Thanks all for your replies. The working syntax is:
UPDATE exp_channel_data AS t1,
(
SELECT
entry_id as ei2, child_entry_id, parent_entry_id, field_id_46 as f46,field_id_47 as f47
FROM
exp_channel_data JOIN exp_playa_relationships ON entry_id=child_entry_id AND parent_field_id = 34) AS t2
SET t1.field_id_60 = f46, t1.field_id_61 = f47
WHERE t1.entry_id=parent_entry_id;
Or in a more classic syntax, you need to adjust to your own foo & bar attributes, but use something like the following:
update exp_channel_data t1
set (t1.field_id_60,t1.field_id_61) = (
select t2.field_id_46 , t2.field_id_47
from exp_channel_data t2
where 1=1
and t2.entry_id = 'x'
and /* ENTER YOUR t1-t2 join condition here */
)
where 1=1
and t1.entry_id = y
;
But since you are MySQL I don't believe it supports compound subquery. As such:
update exp_channel_data t1
set t1.field_id_60 = (
select t2.field_id_46
from exp_channel_data t2
where 1=1
and t2.entry_id = 'x'
and /* ENTER YOUR t1-t2 join condition here */
) , t1.field_id_61 = (
select t3.field_id_47
from exp_channel_data t3
where 1=1
and t3.entry_id = 'x'
and /* ENTER YOUR t1-t3 join condition here */
)
where 1=1
and t1.entry_id = y
;