SQL: Move column data to other table in same relation - mysql

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

Related

How do I update if exist, else insert in MySQL

I have table with AUTO_INCREMENT field defined as PRIMARY_KEY.
I have columns like:
vote_id,vote_user_id,vote_ask_id,vote_comment_id,vote_post_id,vote_type,vote_status
I want to INSERT new records but before I do that I want to check if there is a row with columns(vote_user_id,vote_ask_id,vote_type) as same as the new data I want INSERT.
CONDITIONS:
IF ROW EXISTS
THEN UPDATE tableName SET vote_status=new_value, vote_time=new_time
ELSE
INSERT NEW RECORDS
I have searched the internet and learnt about MySQL ..ON DUPLICATE KEY UPDATE.
I have realize this statement will not be helpful to my task since it only checks for DUPLICATE KEY(...PRIMARY_KEY or UNIQUE FIELD).
I have learnt also on MySQL REPLACE INTO ...and likewise this will not be helpful to my problem since that is also bind to PRIMARY_KEY or UNIQUE index.
I learnt I could use MERGE....USING...statements
but this was giving me errors so i read more about it and I relised it only work in SQL server (Microsoft)
Please how best can someone help me solve this?
I tried this on MERGE staments:
MERGE {$votes_table} WITH (HOLDLOCK) AS VT
USING ({$Qid},{$vote_type},{$CUid},{$vote_status}) AS VTS (vote_ask_id,vote_type,vote_user_id,vote_status)
ON( VT.vote_ask_id = VTS.vote_ask_id AND VT.vote_user_id=VTS.vote_user_id AND VT.vote_type=VTS.vote_type)
WHEN MATCHED THEN
UPDATE SET VT.status=VST.vote_status , VT.vote_time='{$current_time}' WHERE VT.vote_user_id=VTS.vote_user_id AND VT.vote_ask_id=VTS.vote_ask_id AND VT.vote_type=VTS.vote_type
WHEN NOT MATCHED THEN INSERT (vote_ask_id,vote_type,vote_status,vote_user_id,vote_time) VALUES('{$Qid}','{$vote_type}','{$vote_up_status}','{$CUid}','{$current_time}')
In MySQL, use ON DUPLICATE KEY:
INSERT INTO tablename (vote_user_id, vote_ask_id, vote_type, . . . )
VALUES (new_vote_user_id, new_vote_ask_id, new_vote_type . . . )
ON DUPLICATE KEY UPDATE vote_status = VALUES(vote_status), vote_time = VALUES(vote_time);
For this to work, you need a unique index/constraint on the columns that define a unique row:
CREATE UNIQUE INDEX unq_tablename_3 ON tablename(vote_user_id, vote_ask_id, vote_type);
You do want insert ... on duplicate key update: in MySQL, this is the right way to do what you want.
To start with, you need to create a unique constraint on the tuple of concerned columns:
create unique index votes_table_unique_idx
on votes_table(vote_user_id, vote_ask_id, vote_type);
Then, you can do something like:
insert into votes_table(vote_user_id, vote_ask_id, vote_type, vote_comment_id, vote_post_id, vote_status)
values(...)
on duplicate key update
vote_comment_id = values(vote_comment_id),
vote_post_id = values(vote_post_id)
vote_status = values(vote_status)

How update table with duplicate primary key?

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

MySQL syntax error when trying to delete row

I have a simple database query that I can not figure out for some reason.
DELETE * FROM Wishlist WHERE (id, uid) VALUES ("18","34i274o1y24ouy1o4");
This might be just wrong syntax in general. My skills are pretty low when it comes to databases. Any ideas? Just trying to delete a row.
Thanks in advance!
It looks like you were trying to use the INSERT syntax to do a deletion. If you want to remove records with the criteria you gave try this:
DELETE
FROM Wishlist
WHERE id = '18' AND uid = '34i274o1y24ouy1o4'
You should use IN:
DELETE FROM Wishlist WHERE (id, uid) IN ('18','34i274o1y24ouy1o4');
Or just use AND:
DELETE FROM Wishlist WHERE id = '18' AND uid = '34i274o1y24ouy1o4';
DELETE FROM Wishlist WHERE id = '18' AND uid = '34i274o1y24ouy1o4';
In addition to other answers using IN keyword,
DELETE FROM Wishlist where id='18' and uid = '34i274o1y24ouy1o4' also works.
NOTE that you do not need asterisk in DELETE operation.
If id is the primary key, where id = '18'without uid should be enough.
Also, you might want to consider making id an auto increment column with type INT. It is faster to query and you can get rid of the quotes like where id = 18.

MySQL Procedure Insert only if not exists

I'm using MySQL Stored Procedures and I want to insert some rows from a table's database to another table's database through a stored procedure. More specifically from database "new_schema", table "Routers" and field "mac_address" to database "data_warehouse2", table "dim_cpe" and field "mac_address".
This is the code I used in the first insertion, that worked perfectly.
insert into data_warehouse2.dim_cpe (data_warehouse2.dim_cpe.mac_address, data_warehouse2.dim_cpe.ssid)
(select new_schema.Routers.mac_address, new_schema.Routers.ssid from new_schema.Routers, data_warehouse2.dim_cpe);
Now I have more rows in the table "Routers" to be inserted into "dim_cpe" but, since there are rows already there, I want just to insert the new ones.
As seen in other posts, I tried a where clause:
where new_schema.device_info.mac_address != data_warehouse2.dim_cpe.mac_address
and a:
on duplicate key update new_schema.Routers.mac_address = data_warehouse2.dim_cpe.mac_address"
Both didn't work. What's the best way to do this?
Thanks in advance.
You could leave the source table out of the from clause, and use a not exists clause instead:
where not exists
(select mac_address from dim_cpe mac_address = new_schema.Routers.mac_address
and ssid = new_schema.Routers.ssid)
Or you could left join and check whether the fields from dim_cpe are null:
insert into data_warehouse2.dim_cpe
(data_warehouse2.dim_cpe.mac_address, data_warehouse2.dim_cpe.ssid)
(select new_schema.Routers.mac_address, new_schema.Routers.ssid
from new_schema.Routers
left join data_warehouse2.dim_cpe on
new_schema.Routers.mac_address = data_warehouse2.dim_cpe.mac_address
and new_schema.Routers.ssid = data_warehouse2.dim_cpe.ssid
where dim_cpe.mac_address is null and dim_cpe.ssid is null);
Edit to say this is a general SQL solution. I'm not sure if there's a better MySql-specific approach to this.
Edit to show your query:
insert into data_warehouse2.dim_cpe (mac_address, ssid)
select new_schema.Routers.mac_address, new_schema.Routers.ssid
from new_schema.Routers where not exists
(select data_warehouse2.dim_cpe.mac_address from data_warehouse2.dim_cpe
where data_warehouse2.dim_cpe.mac_address = new_schema.Routers.mac_address
and data_warehouse2.dim_cpe.ssid = new_schema.Routers.ssid);

Mysql + Update to table (same time) with Join

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;