Mysql + Update to table (same time) with Join - mysql

I believe this is possible but I haven't done it before.
I need to update 2 tables and I want to use one MySQL command. I also have a join field.
UPDATE static_site_articles SET domainname='a',language='b',url='domain',toptext='c',bottomtext='d' WHERE id='10;
UPDATE static_site_articles_meta SET url='domain',title='e',description='f',keywords='g';
They join with the url='domain'.
How can I construct this into a single UPDATE command line?
A push in the right direction would be greatly appreciated.
thx

http://dev.mysql.com/doc/refman/5.1/de/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;

Related

Convert UPDATE into INSERT INTO ON DUPLICATE KEY UPDATE statement

I have this UPDATE MySQL statement which works fine.
UPDATE table1
Inner Join table2
ON table2.id = table1.gw_id
SET table1.field1=1, table1.field2=2
WHERE table1.nick_no=4 AND table2.addr=123
I would like to convert this UPDATE statement such that it can add a new row if a row with the same table1_nick_no is not found. I believe using INSERT INTO ON DUPLICATE KEY UPDATE is the right way to go. However, I tried for a long time but failed. Adding Inner join and where clause to INSERT INTO ON DUPLICATE KEY UPDATE
How should I convert this UPDATE statement into the corresponding INSERT INTO ON DUPLICATE KEY UPDATE statement?
What you are looking for is a MERGE or an UPSERT (short for UPDATE/INSERT). In mySQL you can achieve an UPSET as described in the following link
http://mechanics.flite.com/blog/2013/09/30/how-to-do-an-upsert-in-mysql/

Insert a new record in MySQL table at start

I am new to MySQL and learning it to my own. Actually I want to copy a column from a table into my existing table column! suppose that my existing table is:
where pid values are inserted by default!
now i want to copy a column from another table using:
INSERT INTO exist_tab(FirstLevel) SELECT some_col FROM another_table;
so that the values should come inside FirstLevel Column.
but the problem is that the copies values come below the pid values in FirstLevel Column as:
see that the firstlevel comes below! what is wrong with it? I need the "H" value against 19 but i dont want to use wild cards just want to copy the new data against old column data
thanks
I am new to this kind a work please can somebody give me any idea how to do it please!
thanks in advance
INSERT and UPDATE is different Command to Perform Different Task.
INSERT :Insert New Record into the table
Update:Update Existing Record in table If Exist.
NOT SURE ABOUT IT:(i'm Not Familiar With MYSQL)
Update a set
a.FirstLevel=b.some_col
from
exist_tab a join another_table b on a.Id=b.Id
Or You can Try :
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table where Id=a.Id)
EDIT2:
update exist_tab a set a.FirstLevel=
(select top 1 some_col from another_table)
You Can Find Here.
You are using INSERT statement here. INSERT will create a new record in the table. You have to use UPDATE for updating a particular column in the existing table like this:
UPDATE exist_tab
SET FirstLevel = (SELECT some_col FROM another_table)
If you want any conditional update then you can use JOIN like this:
UPDATE exist_tab a
LEFT JOIN another_table b ON
a.pid = b.id
SET FirstLevel = a.some_col;

MySQL insert on duplicate key; delete?

Is there a way of removing record on duplicate key in MySQL?
Say we have a record in the database with the specific primary key and we try to add another one with the same key - ON DUPLICATE KEY UPDATE would simply update the record, but is there an option to remove record if already exists? It is for simple in/out functionality on click of a button.
It's a work-around, but it works:
Create a new column and call it do_delete, or whatever, making it a tiny-int. Then do On Duplicate Key Update do_delete = 1;
Depending on your MySQL version/connection, you can execute multiple queries in the same statement. However, if not, just run a separate query immediately afterwords. Either way, the next query would just be: Delete From [table] Where do_delete = 1;. This way, if its a new entry, it will not delete anything. If it was not a new entry, it will then mark it for deletion then you can delete it.
Use REPLACE INTO:
replace into some_table
select somecolumn from othertable
will either insert new data or if thr same data exist will delete the data and insert the new one
The nearest possible solution for the same is REPLACE statement. Here is the documentation for REPLACE.
A similar question was asked on MySQL Forums and the recommended(and only) answer was to use REPLACE.
to be more clear with mySql:
values can be from same table:
replace into table1 (column1,column2) select (val1,val2) from table1
or
values can be from another table:
replace into table1 (column1,column2) select (val1,val2) from table2
or
values can be from any table with condition:
replace into table1 (column1,column2) select (val1,val2) from table1 where <br>column3=val3 and column4=val4 ...
or
also remember values can be static with table name for namesake:
replace into table1 (column1,column2) select (123,"xyz") from table1
no error will be thrown even if the update results in duplicate entry, as it will be replaced.
(remember) only autoincrement value will be increased;
and
if you have column with data-type "TIMESTAMP" with "on update CURRENT_TIMESTAMP", it will have no effect;
Yes of course there is a solutions in MySQL for your problem.
If you want to delete or skip the new inserting record if there already a duplicate record exists in the key column of the table, you can use IGNORE like this:
insert ignore into mytbl(id,name) values(6,'ron'),(7,'son');
Here id column is primary key in the table mytbl. This will insert multiple values in the table by deleting or skipping the new duplicate records.
Hope this will fulfill your requirement.

SQL: Move column data to other table in same relation

I'm wondering if it's possible to move all data from one column in table to another table.
Here's what i'm trying to do:
Table 1 : users - columns trying to read+move = oauth_access_key and oauth_access_secret
Table 2 : account_users - target columns: oauth_token_key, oauth_token_secret
The relation key between these tables is "user_id".
Is this possible in one query? I know this is easily done in PHP, but i'm wondering if this can be done in plain SQL.
Thanks in advance.
UPDATE users, account_users
SET account_users.oauth_token_key=users.oauth_access_key,
account_users.oauth_token_secret = users.oauth_access_secret
WHERE account_users.user_id=users.user_id;
You can use JOIN syntax on MySQL Update.
I think the answer you are looking for is
INSERT INTO `account_users` (`user_id`, `oauth_token_key`, `oauth_token_secret`)
SELECT `user_id`, `oauth_access_key`, `oauth_access_secret` FROM `user`
ON DUPLICATE KEY UPDATE
`oauth_token_key` = VALUES(`oauth_token_key`),
`oauth_token_secret` = VALUES(`oauth_token_secret`);
EDIT
I am assuming you want to move data as in 'put it somewhere it hasn't been yet'.
EDIT2
Here is a documentation on VALUES(): http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html#function_values
Since the title is SQL, and not DB specific... here's a solution for those who stumble upon this while searching for Postgres:
UPDATE account_users
SET oauth_token_key = users.oauth_access_key,
oauth_token_secret = users.oauth_access_secret
FROM profiles
WHERE account_users.user_id = users.user_id;
INSERT INTO account_users (user_id, oauth_token_secret)
SELECT users.user_id, users.oauth_access_secret FROM users
ON DUPLICATE KEY UPDATE account_users.oauth_token_secret = users.oauth_access_secret

UPDATE record if present; else INSERT

I want to update a record which may or may not be present in a table. If it is not present in the database then it will be inserted.
To prevent from select I am using UPDATE statement first and checking affected_rows > 0 if not then I am inserting this record into the table.
I was wondering if there is a better way to do this?
You could use INSERT ... ON DUPLICATE KEY UPDATE syntax:
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
http://dev.mysql.com/doc/refman/4.1/en/insert-on-duplicate.html
The difference between this and REPLACE (Femaref's answer) is that REPLACE will delete the old row and then insert a new row if a key is duplicated, while this will update the existing row.
Use Replace instead of Insert.
http://dev.mysql.com/doc/refman/5.0/en/replace.html