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'
Related
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
I have two tables in the mysql as follows:
Table_A
Table_B
I need to join Table_B to Table_A and add the values of BalAmountLC to Table_A where the GLAccount equals. I also need to update other GLAccount values to Table_A which is not exist in Table_A.
Anyone please advise me which is the best way to do it. I feel like I need to use RIGHT JOIN. But I am not sure about that.
I am stuck with the following query:
SELECT Table_A.*,Table_B.*,Table_A.BalAmountLC+Table_B.BalAmountLC as sum FROM Table_A RIGHT OUTER JOIN Table_B ON Table_A.GLAccount=Table_B.GLAccount
Thanks.
So basically you need to INSERT all GLAccount in Table_B to Table_A if it does not exit in Table_A. If exists, you want to add BalAmountLC to Table_A
UPDATE Table_A INNER JOIN Table_B ON (Table_A.GLAccount = Table_B.GLAccount)
SET Table_A.BalAmountLC = Table_A.BalAmountLC + Table_B.BalAmountLC;
INSERT INTO Table_A SELECT Table_B.* FROM Table_B LEFT JOIN Table_A
ON (Table_B.GLAccount = Table_A.GLAccount) WHERE
Table_A.GLAccount 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_%'
I have the following sql query:
UPDATE
(SELECT * FROM table_A INNER JOIN table_B
ON table_A.id=table_B.a_fk
WHERE table_A.batch=10) AS TBL_1
SET TBL_1.b_name = "test" WHERE TBL_1.a_fk = 67532;
When i run it, i get the following error message:
The target table TBL_1 of the UPDATE is not updatable.
I need to update the column 'b_name' in table_B where the batch value is 10 in table_A.
Any help is most appreciated.
update table_B b
set b.b_name = 'test'
where b.a_fk in
(
select id from table_A where batch=10
)
DELETE FROM table_a WHERE id IN(
SELECT table_a.id AS id FROM table_a, table_b
WHERE table_a.object_id = 1 AND table_a.code = 'code'
AND table_a.code = table_b.code
AND table_b.id = table_a.b_id
AND table_b.table = 'testTable')
This is a (somewhat simplified) query I want MySQL to execute. I read on other pages of stackoverflow that this wasn't supported and that it's solvable by using JOINS. How could this be 'transcribed' to a query using JOINS? I find it hard to do so, because I've never tried creating DELETE queries with more than one table.
You can't delete from a table and reference the same table in a subquery — just a limitation of MySQL. Something like the following should work:
DELETE FROM table_a
USING table_a
INNER JOIN table_b
ON table_a.code = table_b.code
AND table_b.id = table_a.b_id
AND table_b.table = 'testTable'
WHERE table_a.object_id = 1
AND table_a.code = 'code'
The important part is USING. If you just join the two tables, you'll delete records from both. USING tells MySQL to use these tables for processing, but only delete from the tables in the FROM clause.
http://dev.mysql.com/doc/refman/5.0/en/delete.html
This is a common MySQL issue, use a temporary table between the select and update/delete:
DELETE FROM table_a WHERE id IN
(select id from
(SELECT table_a.id AS id FROM table_a, table_b
WHERE table_a.object_id = 1
AND table_a.code = 'code'
AND table_a.code = table_b.code
AND table_b.id = table_a.b_id
AND table_b.table = 'testTable')
) tempTable
There are two (slightly different) syntaxes for deleting from mutliple tables. Here's the one without USING:
DELETE a
FROM
table_a AS a
INNER JOIN
table_b AS b
ON b.code = a.code
AND b.id = a.b_id
WHERE
a.object_id = 1
AND a.code = 'code'
AND b.`table` = 'testTable' --- Do you actually have a column named "table"?
My Way ... If you could use that!
DELETE FROM table_a WHERE id IN(SELECT id FROM(SELECT id FROM table_a WHERE userid=99 GROUP BY mobile HAVING COUNT(mobile) > 1) as t2)