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

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%'

Related

Specific query optimization on MySQL

I have a table into MySQL DB (version 5.1.x), with the name table. Its columns are:
id, double_col_index1, double_col_index2, flag, col_index_1, a_date_col, col_index_2, col_index_3 with the following indexes:
a single index on double_col_index1 and double_col_index2
an index on col_index_1
an index on col_index_2
an index on col_index_3
Now, I have the following query:
UPDATE `table` t1 INNER JOIN
(SELECT t2.id FROM `table` t2
WHERE t2.double_col_index1 = 'fake_value1'
AND t2.double_col_index2 = 'fake_value2'
AND flag = 'true'
AND (col_index_1 = '' OR a_date_col < '1920-11-10 00:00:00')
AND
(SELECT count(t3.id) FROM `table` t3
WHERE t3.double_col_index1 = 'fake_value1'
AND t3.double_col_index2 = 'fake_value2'
AND t3.col_index_2 = t2.col_index_2
AND t3.col_index_3 = 'fake_col_index_3_1') > 0
AND
(SELECT count(t4.id) FROM `table` t4
WHERE t4.double_col_index1 = 'fake_value1'
AND t4.double_col_index2 = 'fake_value2'
AND t4.col_index_2 = t2.col_index_2
AND t4.col_index_3 = 'fake_col_index_3_2') > 0) tbl
ON t1.id = tbl.id SET col_index_1 = 'fake_value';
Question: I would need to improve this query, to give a better performance if possible. Would anyone have any suggestions on this?
One idea would be to use instead of INNER JOIN an t1.id in (.... What is your advice on this?
Basic idea of the join would be to do it as follows.
UPDATE `table` t1
INNER JOIN
(
SELECT t2.id
FROM `table` t2
INNER JOIN
(
SELECT DISTINCT col_index_2
FROM `table`
WHERE double_col_index1 = 'fake_value1'
AND double_col_index2 = 'fake_value2'
AND col_index_3 = 'fake_col_index_3_1'
) t3
ON t3.col_index_2 = t2.col_index_2
INNER JOIN
(
SELECT DISTINCT col_index_2
FROM `table`
WHERE double_col_index1 = 'fake_value1'
AND double_col_index2 = 'fake_value2'
AND col_index_3 = 'fake_col_index_3_2'
) t4
ON t4.col_index_2 = t2.col_index_2
WHERE t2.double_col_index1 = 'fake_value1'
AND t2.double_col_index2 = 'fake_value2'
AND flag = 'true'
AND (col_index_1 = '' OR a_date_col < '1920-11-10 00:00:00')
) tbl
ON t1.id = tbl.id
SET col_index_1 = 'fake_value';
This may be quite a bit quicker but will depend on many factors. MySQL will not use indexes for joining against the sub queries.
However there is a big issue with this (and your existing query) in MySQL
http://dev.mysql.com/doc/refman/5.7/en/update.html
Currently, you cannot update a table and select from the same table in a subquery.
There is a way round this, by doing a query on the sub query

MySQL UPDATE script not working

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

why mysql not excute this update join statment

i want to compare two tables by inner join and update the columns in table d_family by edit_27_5 table columns ..but something wrong..i tried more solution but it ditint work good
i see this message ( #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET d_family.city = edit_27_5.city, SET d_family.fa_name = edit_27_5.fa_n' at line 3)
UPDATE d_family INNER JOIN edit_27_5
ON
d_family.fa_id = edit_27_5.fa_id
SET d_family.area = edit_27_5.area,
SET d_family.city = edit_27_5.city,
SET d_family.fa_name = edit_27_5.fa_name,
SET d_family.fa_nationality = edit_27_5.nath,
SET d_family.fa_phone_number = edit_27_5.phone,
SET d_family.fa_mobile_number = edit_27_5.mobile,
SET d_family.fa_account_anstaqram = edit_27_5.anst,
SET d_family.fa_account_mail = edit_27_5.email,
SET d_family.fa_account_twitter = edit_27_5.twit,
SET d_family.fa_account_facebook = edit_27_5.face,
SET d_family.fa_product_desc = edit_27_5.pt_desc,
SET d_family.product_type = edit_27_5.pt_type,
SET d_family.fa_markting_type = edit_27_5.markting
WHERE d_family.fa_id < 7221
UPDATE d_family INNER JOIN edit_27_5
ON
d_family.fa_id = edit_27_5.fa_id
You were missing the joining part
d_family.fa_id = .fa_id
Also its wrong to have set multiple time
So the syntax is
update table1 t1
join table2 t2 on t1.id = t2.id
set
t1.col1 = t2.col1,
t1.col2 = t2.col,
......
In your case it should be something as
UPDATE d_family
INNER JOIN edit_27_5 ON d_family.fa_id = edit_27_5.fa_id
SET
d_family.area = edit_27_5.area,
d_family.city = edit_27_5.city,
d_family.fa_name = edit_27_5.fa_name,
d_family.fa_nationality = edit_27_5.nath,
d_family.fa_phone_number = edit_27_5.phone,
d_family.fa_mobile_number = edit_27_5.mobile,
d_family.fa_account_anstaqram = edit_27_5.anst,
d_family.fa_account_mail = edit_27_5.email,
d_family.fa_account_twitter = edit_27_5.twit,
d_family.fa_account_facebook = edit_27_5.face,
d_family.fa_product_desc = edit_27_5.pt_desc,
d_family.product_type = edit_27_5.pt_type,
d_family.fa_markting_type = edit_27_5.markting
WHERE d_family.fa_id < 7221

Target table not Updatable error

I need to run this query :
UPDATE (
SELECT r.*
FROM booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON (r.st_from = new.from_t)
and (r.st_to = new.to_t)
and r.date = '2013-04-16'
) temp
SET temp.seat_ac = temp.seat_ac-5
but on execution it gives an error:
#1288-The target table temp of the UPDATE is not updatable.
Any solutions?
I think your UPDATE syntax is incorrect. See if this works:
UPDATE booked r
INNER JOIN (
SELECT a.st_code as from_t
, b.st_code as to_t
FROM `stops_at` a
CROSS JOIN `stops_at` b
WHERE (a.stop_no < b.stop_no)
and (a.train_no = b.train_no)
and (a.train_no = '11280')
) new
ON r.st_from = new.from_t
and r.st_to = new.to_t
and r.date = '2013-04-16'
SET r.seat_ac = r.seat_ac-5

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
;