Insert a new record in MySQL table at start - mysql

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;

Related

Problems with INSERT INTO syntax

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;

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);

Moving Data from Table to Table with new Column

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.

Insert column values into other table with JOIN/WHERE clause

I have a simple amateur question.
Table A(login_count) contains all existing userids and their login-count.
Table B(login4buy) contains specific userids and other information.
I want SQL to add the specific login-count from Table A to the specific userid in table B.
This is my try:
INSERT INTO orders_subset
SELECT login_count
FROM login4buy
WHERE login4buy.userid=orders_subset.userid
How can I put the count from Table A into Table B?
I think you want an UPDATE instead of an INSERT
UPDATE lb
SET lb.orders_subset = lc.login_count
FROM login4buy lb
INNER JOIN login_count lc
ON lb.userid = lc.userid
I think you need UPDATE, not INSERT:
UPDATE
orders_subset
JOIN
login4buy
ON login4buy.userid = orders_subset.userid
SET
login4buy.login_count = orders_subset.login_count ;

Insert the same fixed value into multiple rows

I've got a table with a column, lets call it table_column that is currently null for all rows of the table. I'd like to insert the value "test" into that column for all rows. Can someone give me the SQL for this?
I've tried INSERT INTO table (table_column) VALUES ("test"); but that only populates that last row. How do I do all of the rows at once?
You're looking for UPDATE not insert.
UPDATE mytable
SET table_column = 'test';
UPDATE will change the values of existing rows (and can include a WHERE to make it only affect specific rows), whereas INSERT is adding a new row (which makes it look like it changed only the last row, but in effect is adding a new row with that value).
This is because in relational database terminology, what you want to do is not called "inserting", but "UPDATING" - you are updating an existing row's field from one value (NULL in your case) to "test"
UPDATE your_table SET table_column = "test"
WHERE table_column = NULL
You don't need the second line if you want to update 100% of rows.
To update the content of existing rows use the UPDATE statement:
UPDATE table_name SET table_column = 'test';
What you're actually doing is adding rows. To update the content of existing rows use the UPDATE statement:
UPDATE table SET table_column = 'test';
UPDATE `table` SET table_column='test';
The SQL you need is:
Update table set table_column = "test";
The SQL you posted creates a new row rather than updating existing rows.
To create a new empty column and fill it with the same value (here 100) for every row (in Toad for Oracle):
ALTER TABLE my_table ADD new_column INT;
UPDATE my_table SET new_column = 100;