Question is that I am having two rows for each employee and want to merge them into one.
Trying to run these queries without success. Could you help in figuring out the problem. All the field being used are non-constraints.
UPDATE T1
SET T1.rfid = T2.rfid,
T1.genderid = T2.genderid
from employee T1
LEFT OUTER JOIN employee T2 ON (T1.Code = T2.Code)
or
UPDATE T1
SET T1.rfid = T2.rfid,
T1.genderid = T2.genderid
from employee T1, employee T2
WHERE T1.Code = T2.Code
enter image description here
Error being shown is:
MySQL said: Documentation
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 at line 3
the correct syntax is:
UPDATE TABLE_A a
JOIN TABLE_B b
ON a.join_col = b.join_col
SET a.column = b.column
Related
I have a MySQL table containing contact details, and I need to flag duplicates of the column "PHONE1" into another flag column called "DUPLICATE" (ignoring the first, original, row).
I found an SQL tutorial with an example SQL query doing almost exactly what I want here: https://www.sqlservertutorial.net/sql-server-basics/sql-server-update-join/.
I modified the tutorial example, and I can "SELECT" the duplicate rows correctly with the following query:
SELECT t1.ID, t1.`Contact Name`, t1.PHONE1
FROM new_leads_test2 AS t1
INNER JOIN new_leads_test2 AS t2
ON (t1.PHONE1 = t2.PHONE1
AND t1.ID > t2.ID)
this "SELECT" query works 100%, but when I try to alter this query to actually UPDATE the rows (per the tutorial), I get an SQL syntax error. Here's my query that generates the error:
UPDATE t1
SET t1.duplicate = t2.ID
FROM new_leads_test2 AS t1
INNER JOIN new_leads_test2 AS t2
ON (t1.PHONE1 = t2.PHONE1
AND t1.ID > t2.ID)
This results in an SQL error:
#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 'FROM new_leads_test2 AS t1 INNER JOIN new_leads_test2 AS
t2 ON t1.PHONE1 = t2.' at line 3
Can anyone please help me with this?
We might put SET after INNER JOIN because UPDATE Statement, table_reference use before SET
BTW maybe you might not need FROM
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET assignment_list
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
so we might use like below.
UPDATE new_leads_test2 AS t1
INNER JOIN new_leads_test2 AS t2
ON (t1.PHONE1 = t2.PHONE1
AND t1.ID > t2.ID)
SET t1.duplicate = t2.ID
Your example is a SQL Server syntax, not MySQL.
Try with:
UPDATE new_leads_test2 t1
INNER JOIN new_leads_test2 t2
ON (t1.PHONE1 = t2.PHONE1 AND t1.ID > t2.ID)
SET t1.duplicate = t2.ID
I have this sql query:
update edi_file_steps
set
table_A.user_id= table_B.id ,
table_A.message= SUBSTRING_INDEX(table_A.message,'[',1)
FROM
edi_file.steps AS table_A INNER JOIN GU_User as table_B
where
message LIKE '%Downloaded%'AND table_B.login = 'Jack'
But I am getting mysql syntax error. Is there a problem with my syntax?
I am using mysql 5.7.
You can't use FROM in an UPDATE query, you specify the table after the UPDATE statement:
UPDATE edi_file_steps table_A
INNER JOIN GU_User AS table_B
SET
table_A.user_id= table_B.id ,
table_A.message= SUBSTRING_INDEX(table_A.message,'[',1)
WHERE
message LIKE '%Downloaded%'AND table_B.login = 'Jack'
I'm trying to "merge" two tables and have found a few examples but I'm having difficulty applying them as it continues to say I have syntax error:
UPDATE T2
SET payable_id = T1.payable_id, payable_type = T1.payable_type
FROM payments_distributions AS T2
JOIN payables AS T1
ON T1.payments_distribution_id = T2.id
It mentions that the FROM is at an invalid position at the moment.
I'd appreciate the help. Thanks
Move the SET clause to the end and all of the table references after UPDATE.
UPDATE payments_distributions t2
INNER JOIN payables t1
ON t1.payments_distribution_id = t2.id
SET t2.payable_id = t1.payable_id,
t2.payable_type = t1.payable_type;
I am trying to set a column value in MySQL based on the following query.
select * from table1 t1
join table2 t2
on t1.id = u.t1_id
and t2.status = 'verified'
and not exists (
select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id
and t2_2.updated_at > t2.updated_at
)
This query returns the results I want, but when I try to add
SET t1.column_k = 'some value'
to the end, I'm getting an error that simply says You've got a syntax error near set t1.column_k.... check manual corresponding to your version of MySQL.
I'd really like to know how to include a set on the results of this query and am having trouble formulating that. Any help or ideas?
It's difficult and confusing to me I think because of the self join. The eventual plan is to port this query w/the set command into a migration file in rails once I've got it working.
You need an update. Select isn't used for setting values.
update table1 t1 join
table2 t2
on t1.id = u.t1_id and
t2.status = 'verified' and
not exists (select 1
from table2 t2_2
where t2.t1_id = t2_2.t1_id and
t2_2.updated_at > t2.updated_at
)
set t1.column_k = 'some value';
How come this query:
SELECT *
FROM `store_catalog_product_option`
JOIN `store_catalog_product_option_type_value`
WHERE `product_id`=15676
AND `store_catalog_product_option_type_value`.`sku` LIKE '%UNIT_%'
retrieve data.
But replacing
select *
with
delete
as such
DELETE
FROM `store_catalog_product_option`
JOIN `store_catalog_product_option_type_value`
WHERE `product_id`=15676
AND `store_catalog_product_option_type_value`.`sku` LIKE '%UNIT_%'
give syntax error:
SQLSTATE[42000]: Syntax error or access violation: 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 'JOIN store_catalog_product_option_type_value WHERE product_id=15676 AND `sto' at line 1
For multi-table deletes,
For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted.
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
And for LEFT JOIN, you should use something like
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
Your JOIN should have an ON part
You can try somthing like this:-
DELETE FROM `store_catalog_product_option` A
JOIN `store_catalog_product_option_type_value' B
ON A.ID = B.ID
WHERE A.`product_id`=15676
AND B.`sku` LIKE '%UNIT_%'
Alternatively you can use:-
DELETE A
FROM `store_catalog_product_option` A
JOIN `store_catalog_product_option_type_value' B
ON A.ID = B.ID
WHERE A.`product_id`=15676
AND B.`sku` LIKE '%UNIT_%'
...to delete only from store_catalog_product_option
I needed indeed to use a multiple table syntax, including also a ON condition for the JOIN.
Since i wanted to delete rows in both tables , here is my working query, after your help:
DELETE O, V FROM `store_catalog_product_option` O JOIN `store_catalog_product_option_type_value` V on O.option_id=V.option_id WHERE `product_id`=15676 AND V.`sku` LIKE '%UNIT_%'