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
Related
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;
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
If I run the following SQL statement:
SELECT * FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
It gives me all the rows that don't have a matching row in the users table, (because the user has been deleted). But I cannot simply turn this into a DELETE statement:
DELETE FROM `user-data` t1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL
As I get the following 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 't1
LEFT JOIN `users` t2 ON t1.userID = t2.id
WHERE t2.id IS NULL' at line 1
What can I do to delete all the rows in user-data that don't have an existing user in table users?
instead of using LEFT OUTER JOIN and IS NULL try a WHERE NOT EXISTS. Something like this:
DELETE t1
FROM user-data t1
WHERE NOT EXISTS
(
SELECT id FROM FROM users WHERE user-data.userID = users.id)
)
A delete join query is absolutely valid in MySQL. I think the issue is that you did not specify an alias after DELETE. Hence, the following should work:
DELETE t1 FROM `user-data` t1
LEFT JOIN `users` t2
ON t1.userID = t2.id
WHERE t2.id IS NULL
I suspect the need for an alias here arises because it is not clear from which table we want MySQL to do a deletion. Note that using the WHERE NOT EXISTS approach does not require an alias after DELETE.
I seem to remember that you cannot use table aliases in a DELETE. Also you can specify the table where you want to delete from like this:
DELETE `user-data`.* FROM `user-data`
LEFT JOIN `users` ON `user-data`.userID = `users`.id
WHERE `users`.id IS NULL
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_%'
In my mysql I am having t1, t2 tables and I want to update t1's field from t2's field value based on t1's field value match t2's field value
I tried the below but it is not updating. What I did wrongly here
UPDATE t1
INNER JOIN t2
ON t1.name = t2.name
SET t1.age = t2.age
WHERE t1.name IS NOT NULL;
You need to separate the table you want to update from the table your querying, even though it is the same:
UPDATE t1
SET t1.age = t2.age
FROM t1 as tempT1
INNER JOIN t2
ON tempT1.name = t2.name
WHERE tempT1.name IS NOT NULL;
UPDATE
Apparently MySQL is using a different UPDATE JOIN Syntax than other db's. Your initial query seems to use the correct syntax, just to be sure try to alias the table names:
UPDATE t1 temp1
INNER JOIN t2 temp2
ON temp1.name = temp2.name
SET temp1.age = temp2.age
WHERE temp1.name IS NOT NULL;
UPDATE 2
After looking at this a bit longer I'm certain that the WHERE clause is the issue:
WHERE temp1.name IS NOT NULL
You cannot join on null values anyway, so they are filtered out by default. The WHERE clause is somehow interfering with the join.
Try and remove it to see if the UPDATEworks. If you don' t want to execute and update right away simply execute a select with the same JOIN CLAUSE to see which records would be affected.
Here is a general reference to NULL and JOIN:
http://databases.about.com/library/weekly/aa051203a.htm
Here is the SQL Server Reference in compliance with the above: http://msdn.microsoft.com/en-us/library/ms190409.aspx
Could not find a MySQL reference that states this explicitly but I think this is true for all Relational DBs.
I myself also found another way to achieve this same scenario. I am pasting the answer here so that others will get benefit.
UPDATE t1 SET t2_age = (SELECT age FROM t2 BINARY WHERE name = t1.name);
Try this:
update t1
set t1.age = t2.age
from t1
inner join t2 on t1.name = t2.name
where t1.name is not null
Try this
UPDATE t1,t2
SET t1.age = t2.age
FROM t1 as tempT1
INNER JOIN t2 ON tempT1.name = t2.name
WHERE tempT1.name IS NOT NULL;