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
)
Related
Need help with a mysql query. I have 2 tables - table1 and table2. I am trying to update a field in table1 which is not included in table2 - named Status. And I want to update that field with the value of 'A'. table1 and table2 DO have a field in common - named Member_ID. Here is my query that is giving me errors:
UPDATE table1 SET Status='A' WHERE Member_ID=table2.Member_ID;
Is there some type of join that is needed? Any help would be appreciated. Thanks.
cdr6800
This can be done with exists
UPDATE table1 s
SET s.status = 'A'
WHERE EXISTS(select 1 from table2 t
WHERE t.member_id = s.member_id)
You have to JOIN table1 to table2 like this:
UPDATE table1 AS t1
INNER JOIN table2 AS t2 ON t1.Member_ID = t2.Member_ID
SET t1.Status='A'
This will update all table1 records having a Member_ID value that also exists in table2.
I have tabel 1 that has VIN which i want to update.
table 1 and table 2 has OBJ ID and POID (respectively) which are same.
I only know device ID which is present in table 2.
update table.1 set VIN = '5TDKK3DC6BS018229'
from table 2, table 1
where 2.device ID = 'TCAXLcKkt3'
and 2.OBJ = 1.POID;
I am getting SQL command not properly ended.
Make sure you remove the semi-colon at end of the query since some databases will complain if its there in TOAD.
If you are using SQL Server then following query will work.
UPDATE table1
SET VIN = '5TDKK3DC6BS018229'
FROM table1
INNER JOIN table2 ON table1.POID = table2.OBJ
WHERE table2.deviceID = 'TCAXLcKkt3'
If you want the same query for MySQL, then use the one below.
UPDATE table1 a
JOIN table2 b
ON a.poid = b.obj
SET a.vin = '5TDKK3DC6BS018229'
WHERE b.deviceid = 'TCAXLcKkt3'
If you are using Oracle, then use the query as below.
UPDATE table1
SET table1.vin = '5TDKK3DC6BS018229'
where exists (SELECT table2.obj
FROM table2
WHERE table2.obj = table1.poid
AND table2.deviceid = 'TCAXLcKkt3')
Error message looks like you are using Oracle.
If am not wrong this is what you are looking for
UPDATE table1
SET vin = '5TDKK3DC6BS018229'
WHERE EXISTS (SELECT 1
FROM table2 B
WHERE table1.obj = B.poid
AND B."device id" = 'TCAXLcKkt3')
In Oracle to update table from another table using Join try the below syntax
UPDATE
(SELECT A.VIN
FROM table1 A
INNER JOIN table2 B
ON A.OBJ = B.POID
WHERE B."device ID" = 'TCAXLcKkt3'
) t
SET T.VIN = '5TDKK3DC6BS018229'
I have an update statement that is working in Mysql.(update multiple records, based on the column in another table)
UPDATE `table1` m
INNER JOIN
(SELECT cse_cd FROM table2 WHERE clsf_ind='NC') t
ON m.cse_cd = t.cse_cd
SET m.ST_CD = 'QUEST03'
However, it does not work in Oracle.
Can somebody help on this.
This should do the job:
UPDATE table1
SET ST_CD = 'QUEST03'
WHERE EXISTS
(SELECT 1
FROM table2
WHERE clsf_ind='NC' AND table1.cse_cd=table2.cse_cd);
I have two similar tables table_a and table_b
table_a is my current data and I need to update this info using table_b which is a temporary table. The only difference between the two is table_a has a password field that table_b will not have.
I am trying to do a couple of things.
1.Compare the data based on the "user_id" field.
2. If there is a user_id not found in table_b but there is one in table_a remove that row in table_a
3. If there is a user_id in table_b that is not found in table_a add that row of data to table_a
4. If the user_id is found in both then check two of the fields "email" and "job_code" and make sure they are both whatever table_b says.
Should I do this in separate MySQL statements in the order I have numbered above? below is my try at the above statements. Any help or troubleshoot is greatly appreciated. Thanks!
Statement 1:
SELECT * FROM table_a
WHERE table_a.user_id
NOT IN (
SELECT table_b.user_id
FROM table_b
WHERE table_a.user_id=table_b.user_id
) // But how do I delete those records that are selected?
Statement 2:
SELECT * FROM table_b
WHERE table_b.user_id
NOT IN (
SELECT table_a.user_id
FROM table_a
WHERE table_a.user_id=table_b.user_id
) //How do I Insert these records that I have in table_b but not in table_a
Statement 3:
SELECT email FROM table_b
WHERE table_b.user_id = table_a.user_id,
AND table_b.email != table_a.email //now I need to update table_a with the emails from table_b
Statement #1
DELETE A.*
FROM table_a A
LEFT JOIN table_b B
USING (user_id)
WHERE B.user_id IS NULL;
Statement #2
INSERT INTO table_b (column1,column2,...,columnN)
SELECT A.column1,A.column2,...,A.columnN
FROM table_b B LEFT JOIN table_a A
USING (user_id)
WHERE A.user_id IS NULL;
Statement #3
UPDATE table_a A INNER JOIN table_b B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
UPDATE 2012-06-19 11:54 EDT
A and B are just aliases for the table.
For example, the query of Statement #3 could have been written like the following:
UPDATE table_a as A INNER JOIN table_b as B USING (user_id)
SET A.email = B.email,A.job_code = B.job_code;
or with no aliases whatsoever, like this:
UPDATE table_a INNER JOIN table_b USING (user_id)
SET table_a.email = table_b.email,table_a.job_code = table_b.job_code;
The solution of the first problem ::
1)
delete FROM table_a
left join table_b
on
table_a.user_id=table_b.user_id
and table_a.user_id is null
I would like to replace a column of data in a table.
TableA
Uid - int
AnotherUid - int
TableB
Uid - int
TableA.uid = Table.B uid
And I am trying to replace the TableB.Uid with TableA.AnotherUid
Select * from TableB a, TableA b where a.uid=b.uid
update TableB set a.uid=b.AnotherUid
I got a SQL syntax error from MySQL at TableB set a.uid=b.AnotherUid.
Please kindly help.
UPDATE TableB T
SET T.uid =
(SELECT AnotherUid
FROM TableA A
WHERE A.uid = T.uid)
UPDATE TableB SET TableB.Uid = (SELECT AnotherUid FROM TableA WHERE TableA.Uid = TableB.Uid)
Try this query:
Update TableB, TableA
Set TableB.uid = TableA.AnotherUid
Where TableB.uid = TableA.uid;
For MySQL manual on join in the Update query please refer: http://dev.mysql.com/doc/refman/5.0/en/update.html and see this example in their doc:
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;