I am trying to insert data from one table into another. However, I need it to insert where two other values of the table are equal. I've tried:
INSERT INTO table_one(admin_id)
SELECT id FROM admin
WHERE table_one.employee_code = admin.employee_code
I get an error like:
Unknown column 'admin.employee_code' in 'where clause'
What's the proper way to handle this situation?
If this question has been asked somewhere else ( I'm sure it has, but I wasn't sure what to ask and couldn't find it ) please link me to it.
Thank you
EDIT
Turns out it was a case where I should have updated.
Here is the MySQL syntax that worked.
UPDATE table_one as t1 JOIN
admin as a
ON t1.employee_code = a.employee_code
SET admin_id = a.id;
I am guessing you want to update the table, not insert a new record:
UPDATE table_one t1 JOIN
admin a
ON t1.employee_code = a.employee_code
SET admin_id = admin.id;
Related
I want to query one table to see whether there exist any rows with 'A' type.
so I use this sql:
SELECT EXISTS(select * from %T where type = 'A');
then I need to update another table's column value to the above result. In order to prevent an insert with 'A' type happen during update, I am thinking to use a lock. but lock is very expensive, is there other alternative way to do this without locking?
If we have to use lock, I am thinking if table has already had type A, there is no need to lock insert during update because the result will still be 1. only prevent insert when there is no row with type A. How to do that?
Thanks!
Since you want to update a table when there is a type 'A', normally I would suggest you do the update or check every time right after inserting or deleting a row.
Or you could do something like this:
UPDATE table1 t1
LEFT JOIN table2 t2 ON t1.type = t2.type
SET t1.column1 = 1
WHERE t2.type IS NOT NULL
or
UPDATE table1 t1
SET t1.column1 = 1
WHERE EXISTS (
SELECT ...
FROM table2
WHERE type = 'A'
);
Please give me more details such as what is your table like and what exactly you want to do so we can discuss further.
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);
I have been trying to move some data which relates to a specific column from one table to another. They both have a matching objectID.
So what I am trying to do is:
TABLE 1
ObjectID
Field with Data
TABLE 2
ObjectID
FIELD with NEW column
So the object ID relate to each other. All it is I am trying to do is move the data from Table 1 to Table 2 with the new column.
I have tried following but cant seem to get it all working. Is there anything that can be suggested that may help or point in me the right direction.
update Table2 a
Set a.NewColumn = (Select *
From Table1 b WHERE a.OBJECTID = b.OBJECTID
)
Hm, maybe I don't get it but why don't you use a INSERT?
INSERT INTO TableB(...columns...)
SELECT ...columns...
FROM TableA
You can use join update syntax for this, need to make sure that the Table2 already has data and you are updating a new column in there from Table1
update Table2 t2
join Table1 t1 on t1.OBJECTID = t2.OBJECTID
set t2.NewColumn = t1.Field
You can write query like this.
INSERT INTO Table2(ObjectID,Field)
SELECT ObjectID,Field
FROM Table1.
And you can put any default value in extra column.
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;
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