MySQL UPDATE script not working - mysql

I am trying to run an UPDATE script across two tables, but it isn't working. Can anyone tell me what I'm doing wrong:
UPDATE adb_addressbook a, a_table b
SET a.gtxr2_product_family = b.product_family,
SET a.gtxr2_product_family_factory = b.factory,
SET a.gtxr2_product_family_model = b.model,
SET a.gtxr2_product_family_size = b.size
WHERE a.contact_id = b.contact_id;

This is how it should be
update adb_addressbook a
JOIN a_table b on b.contact_id = a.contact_id
SET a.gtxr2_product_family = b.product_family,
a.gtxr2_product_family_factory = b.factory,
a.gtxr2_product_family_model = b.model,
a.gtxr2_product_family_size = b.size;
DEMO

Your syntax is wrong.. It should be something like that
UPDATE A
SET A.NAME = B.NAME
FROM TableNameA A, TableNameB B
WHERE A.ID = B.ID

Related

Inner join instead of what I have now

The query below does what I want but takes a long time to finish. I have been trying to write it with INNER JOIN instead but can not figure out how to do it.
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = (
SELECT aaa_items.amount
FROM aaa_items
WHERE aaa_items.customer = cf_ab_companies.model_id
AND aaa_items.sku = 10
);
cf_ab_companies.model_id is unique value in the table.
There can be several records where aa_items.customer = 314 , but aa_items.customer = 314 AND aa_item.sku = 10 may only occur once.
Hope you understand what I mean. Thanks for all help.
UPDATE Multiple-table syntax can be found https://dev.mysql.com/doc/refman/8.0/en/update.html
using this your query would look like
UPDATE cf_ab_companies join
aaa_items on aaa_items.customer = cf_ab_companies.model_id
SET cf_ab_companies.col_330 = aaa_items.amount
WHERE aaa_items.sku = 10
;
Here is the MySQL UPDATE ... JOIN ... SET syntax:
UPDATE cf_ab_companies c
INNER JOIN aaa_items i ON i.customer = c.model_id AND i.sku = 10
SET c.cf_ab_companies.col_330 = i.aaa_items.amount
update cf_ab_companies c
join aaa_items a on a.customer = c.model_id
set c.col_330 = a.amount
where a.sku = 10
......
I believe an UPDATE FROM can help:
UPDATE cf_ab_companies
SET cf_ab_companies.col_330 = aaa_items.amount
FROM cf_ab_companies INNER JOIN aaa_items
ON cf_ab_companies.model_id = aaa_items.customer AND aaa_items.sku=10

How to create SQL-Query to update data in tables?

I'm trying to update data in my table.
I have two tables, t1 and t2
T1
T2
I'm want to do, that if t1 have id_avito = null and all_usl_name = %usl_name1% and all_tel = %tel1%, and t2 have id != null, usl_name = %usl_name1% and tel = %tel1%
For example, t1 after execute query must to look like that
update people.t1, people.t2
set
id_avito = people.t2.id,
lnk_avito = people.t2.link,
all_price = people.t2.price,
all_date = people.t2.date,
all_adr = people.t2.adr,
all_usl_name = people.t2.usl_name
where id_avito != people.t2.id
and all_tel= people.t2.tel
and all_usl_type = people.t2.usl_type
I try to do like this, but it is not working
UPD
EXAMPLE: tables. Table before update, after update, and second table
Try with update join also you need to use like operator where you are searching with string
update people.t1 a
join people.t2 on id_avito != people.t2.id
and all_tel= people.t2.tel
and all_usl_type = people.t2.usl_type
set
id_avito = people.t2.id,
lnk_avito = people.t2.link,
all_price = people.t2.price,
all_date = people.t2.date,
all_adr = people.t2.adr,
all_usl_name = people.t2.usl_name
Please try this.
For SQL
UPDATE A
SET
A.id_avito = B.id,
A.lnk_avito = B.link,
A.all_price = B.price,
A.all_date = B.date,
A.all_adr = B.adr,
A.all_usl_name = B.usl_name
FROM
people.t1 A
INNER JOIN people.t2 B
ON A.id_avito != B.id
AND A.all_tel= B.tel
AND A.all_usl_type =B.usl_type
where ISNULL(A.id_avito,'') ='' and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and B.id is not null and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'
FOR MYSQL
UPDATE people.t1 a
INNER JOIN people.t2 B
ON A.id_avito != B.id
AND A.all_tel= B.tel
AND A.all_usl_type =B.usl_type
SET
A.id_avito = B.id,
A.lnk_avito = B.link,
A.all_price = B.price,
A.all_date = B.date,
A.all_adr = B.adr,
A.all_usl_name = B.usl_name
where IFNULL(A.id_avito,'') = '' and A.all_usl_name like '%usl_name1%' and A.all_tel like '%tel1%' and IFNULL(B.id,0) <> 0 and B.usl_name like '%usl_name1%' and B.tel like '%tel1%'

Update: You can't specify target table 'table' for update in FROM clause

I have two queries for oracle. And i need to modify them for mysql.
First query:
UPDATE tec_onoff_file a
SET emailtype = 'MIGR'
WHERE EXISTS
(SELECT 1
FROM tec_onoff_file
WHERE emailtype = 'MIGR'
AND a.acctnbr = acctnbr
AND a.magabbr = magcodes);
I changed it to
update tec_onoff_file t1
join tec_onoff_file t2
on t2.emailtype='MIGR'
and t1.acctnbr=t2.acctnbr
and t1.magabbr=t2.magcodes
set t1.emailtype='MIGR';
and it works.
But second query is harder for me
update tec_onoff_file a
set emailtype = 'REIN'
where transtype = 'REIN'
and curracctnbr not in (select curracctnbr from tec_onoff_file b
where emailtype ='RENW'
and a.curracctnbr=b.curracctnbr);
Could someone help with it? I'm trying change it like first query with JOIN, but it fails, i don't know how to do it.
UPDATE tec_onoff_file a
LEFT
JOIN tec_onoff_file n
ON b.curracctnbr = a.curracctnbr
AND b.emailtype ='RENW'
SET emailtype = 'REIN'
WHERE a.transtype = 'REIN'
AND b.transtype IS NULL;

MySql using user defined variables in an update statement

How can I use user defined variables in a MySql update statement? By user defined variables, i'm referring to the #v:=.
The statement below works fine without the user defined variables, but not with them.
update amga a left join amgb b on a.itemTempId = b.itemTempId
set #i:= concat(a.itemCountry,'-',a.id), a.itemId = #i, b.itemId = #i
where a.itemId is null or b.itemId is null;
I'll be using this with php PDO later.
This works, but does use user defined variables
update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = concat(a.itemCountry,'-',a.id), b.itemId = concat(a.itemCountry,'-',a.id)
where a.itemId is null or b.itemId is null;
Try following syntax:
update amga a left join amgb b on a.itemTempId = b.itemTempId
set a.itemId = (#i:= concat(a.itemCountry,'-',a.id)), b.itemId = #i
where a.itemId is null or b.itemId is null;
SQLFiddle demo

mysql update + ( where + join) syntax

$rs = mysql_query("
SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1");
while($r = mysql_fetch_assoc($rs))
$ids[] = $r['id'];
$rs = mysql_query("
UPDATE a
SET p2=2
WHERE id IN (".implode(",",$ids).")");
How to do this in one query?
UPDATE a
JOIN b ON a.id=b.id AND a.p1 = 1 AND b.p1 = 1
SET a.p2 = 2
You can do it using INNER JOIN which performs better than IN clause:
UPDATE a
INNER JOIN b
ON b.id=a.id
AND b.p1 = 1
AND a.p1 = 1
SET p2 = 2;
UPDATE a
SET p2=2
WHERE id IN (SELECT a.id
FROM a
JOIN b ON b.id=a.id
WHERE
b.p1=1 AND
a.p1=1)