I need some help with an MySQL update. I have 2 tables as follows:
**Inventory**
vmvcenter
vmid
hostname
**Guest_import**
vmvcenter
vmid
discrepancy
I need to put a 'YES' in the guest_import.discrepancy column if the record does not exist in the inventory table. The key between the tables should be CONCAT(vmvcenter,vmid).
update guest_import g
left join Inventory i on i.vmvcenter = g.vmvcenter and i.vmid = g.vmid
set discrepancy = 'YES'
where i.vmvcenter is null
Related
I trying to update a table with another table of other database, I the last updated I created a function to save more than one category in one product, so, I when a run the old script to update my table, a constraint error appears. I understand the situation and why that is happenning, but how I allow the update with the table with duplicates data? Is there a way to disable the constraint?
My query
UPDATE novourbano.oc_product_to_category oc
INNER JOIN erp_product_category erp ON oc.product_id = erp.erp_productid
SET oc.product_id = erp.erp_productid,
oc.category_id = erp.erp_categoryid
WHERE oc.product_id <> 0
I try to use that:
SET GLOBAL FOREIGN_KEY_CHECKS=0;
But still not working. Any suggestion? Thank's in advance!
If the table name reflects the table purpose the primary key should be
PRIMARY KEY(product_id, category_id)
in order to avoid duplicates like several rows with the same product_id and category_id.
You can use IGNORE for this update:
UPDATE IGNORE novourbano.oc_product_to_category oc
INNER JOIN erp_product_category erp ON oc.product_id = erp.erp_productid
SET oc.product_id = erp.erp_productid,
oc.category_id = erp.erp_categoryid
Any SQL gurus out there who can rewrite the following query:
UPDATE cmsTemplate
SET master = NULL
WHERE master IS NOT NULL
AND master NOT IN (SELECT nodeId
FROM
( SELECT * FROM cmsTemplate a) b
)
So that it does not produce the following error:
You can't specify target table 'cmsTemplate' for update in FROM clause
Issue documented here:
http://dev.mysql.com/doc/refman/5.6/en/update.html
Thanks,
Steve
UPDATE: Description of query
The idea of the query is to do as follows:
SET the master field TO NULL
WHERE the master field IS NOT NULL
AND WHERE the master field IS NOT EQUAL TO ANY of the values for the nodeId field records (within the same table)
You can use update with join:
update cmsTemplate c1 left join cmsTemplate c2
on c1.`master` = c2.nodeId
set c1.`master` = null
where c2.nodeId is null;
I am using postgres as my database and have 2 tables
accounts
id | name | status
accountOwner
account_id | user_id
I have user_id(accountOwner table) and status(accounts table) to be updated based on accountid I have. Is it possible to update both the tables in 1 query? I tried the following
UPDATE accounts,accountOwner SET accounts.status='active', accountOwner.user_id=3 WHERE accounts.id=accountOwner.account_id AND accountOwner.account_id = 1;
No, you cannot update two tables at once. The documentation says:
UPDATE -- update rows of a table
However, you can use a FROM clause, for example:
UPDATE accounts SET status = 'active'
FROM accountOwners
WHERE accounts.id = accountOwners.account_id
AND accountOwners.account_id = 1;
I am trying to swap two columns on two different rows (role). I know I could do this by setting user variables, but I was trying to do it all in one query if possible.
My query is:
UPDATE table AS t1, table AS t2
SET t1.role = 'standby', t2.role = 'primary'
WHERE t1.sysid = t2.sysid AND t1.role = 'primary' AND t2.role = 'standby';
The table structure is
sysid | role | devid
1 | primary | dev1
1 | standby | dev2
Primary key is sysid,role
The problem seems to be the conflict between the primary keys at the time of the transaction, with error: Error Code: 1706. Primary key/partition key update is not allowed since the table is updated both as 't1' and 't2'.
Since MySQL processes left to right, I tried setting a temporary value to t1 first to avoid the conflict, but it still failed with the same error:
SET t1.role = 'tmp', t2.role = 'primary', t1.role = 'standby'
Is there a way to achieve this with MySQL?
You can just use a temporary value to swap the roles. Pick a value that doesn't exist yet to not cause key violation. Use transactions for data integrity.
START TRANSACTION;
UPDATE table SET role = 'temprole' WHERE sysid = 1 and role = 'primary';
UPDATE table SET role = 'primary' WHERE sysid = 1 and role = 'standby';
UPDATE table SET role = 'standby' WHERE sysid = 1 and role = 'temprole';
COMMIT;
It's been a few years since I've had to create a SQL statement manually, however is a flipflop possible?
UPDATE table SET role=IF(role='primary', 'secondary', 'primary')
WHERE sysid = 1
ORDER BY role ASC;
UPDATE table AS t1 join table AS t2 on t1.sysid=t2.sysid
SET t1.role = if(t1.role = 'primary' AND t2.role = 'standby',
'standby',
if (t2.role = 'primary' AND t1.role = 'standby','primary',t1.role));
Join both tables and update one depending on conditions
UPDATE
I swapped columns but actually PK doesn't allow to execute the update. Should be set constraints check off before the call
I have a SSIS package that copies data from table A to table B and sets a flag in table A so that the same data is not copied subsequently. This works great by using the following as the SQL command text on the ADO Net Source object:
update transfer
set ProcessDateTimeStamp = GetDate(), LastUpdatedBy = 'legacy processed'
output inserted.*
where LastUpdatedBy = 'legacy'
and ProcessDateTimeStamp is not null
The problem I have is that I need to run a similar data copy but from two sources table, joined on a primary / foreign key - select from table A join table B update flag in table A.
I don't think I can use the technique above because I don't know where I'd put the join!
Is there another way around this problem?
Thanks
Rob.
You can use a join in an update statement.
update m
set ProcessDateTimeStamp = GetDate(),
LastUpdatedBy = 'legacy processed',
somefield = t.someotherfield
output inserted.*
from transfer t
join mytable m
on t.id = m.id
where m.LastUpdatedBy = 'legacy'
and m.ProcessDateTimeStamp is null
and t.ProcessDateTimeStamp is not null
The key is to not alias the fields on the left side of the set but to alias everything else. And use the table alias for the table you are updating after the update key word so it knows which table of the join to update.