I'm trying to insert the result of a Right join into column CATO_NAME of table deal_classification_DM I created.
The result of the select is what I expect and the program run's fine, but data is not updated into the table.
What's wrong?
`INSERT INTO deal_classification_DM
(CATO_NAME)
SELECT
taxonomies.NAME
FROM
taxonomies
RIGHT JOIN
deal_classification_DM ON taxonomies.ID = deal_classification_DM.CATO_ID;
The insertion is done in table deal_classification_DM which I created like this:
CREATE TABLE deal_classification_DM
AS SELECT
deal_taxonomy.DEAL_ID
,deal_taxonomy.TAXONOMY_ID AS TAXO_ID
,taxonomies.NAME AS TAXO_NAME
,taxonomies.PARENT_ID AS CATO_ID
FROM
deal_taxonomy
LEFT JOIN
taxonomies ON deal_taxonomy.TAXONOMY_ID = taxonomies.ID;
ALTER TABLE deal_classification_DM
ADD COLUMN
CATO_NAME TEXT;
What you really need to do is update and not insert, e.g.:
UPDATE deal_classification_DM d
SET d.CATO_NAME = (
SELECT name FROM taxonomies WHERE ID = d.CATO_ID
);
INSERT statement will insert new rows with null values in DEAL_ID, TAXO_ID and CATO_ID columns whereas you only need to update the values of CATO_NAME column as it's a new column.
Related
I have two table with column like this:
Table aggthndet (Reference table)
SELECT `aggthndet`.`idaggdet`,
`aggthndet`.`idagg`,
`aggthndet`.`noakun`,
`aggthndet`.`ketdet`,
`aggthndet`.`pagu`,
`aggthndet`.`prosesagg`,
`aggthndet`.`realisasi`,
`aggthndet`.`iu_id_usr`,
`aggthndet`.`iu_wkt`,
`aggthndet`.`iu_stat`FROM `aggthndet`;
Table aggakundet
SELECT `aggakundet`.`id`,
`aggakundet`.`idaggdet`,
`aggakundet`.`ketdetakun`,
`aggakundet`.`volume`,
`aggakundet`.`hrg_satuan`,
`aggakundet`.`iu_id_usr`,
`aggakundet`.`iu_wkt`,
`aggakundet`.`iu_stat`
FROM `aggakundet`;
The tables are mutually related to each other (relationship one-to-many)
i want insert data into table aggakundet, and update column pagu on table aggthndet, pagu column is the sum of the overall jml_total (alias column) of columns that have the same idaggdet.
sample data
table aggthndet
table aggakundet
In your script, you are having the value of idaggdet in $idaggdet.
Once insertion is done, proceed with UPDATE using the values in $idaggdet
You can take the below query as reference,
UPDATE `aggthndet`
SET `pagu` = `pagu`+1
WHERE `idaggdet` = '$idaggdet';
You can append this update statement in $sql itself.
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 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 added a new column to an existing table(columns acadid,orgid,childid) and now I want to insert values for it.
alter table table1 add new_parent int
insert into table (new_parent) select parent from (select parent
from table2 o inner join table1 ou
on ou.orgid=o.orgunitid) np
Here:
select parent
from table2 o inner join table1 ou
on ou.orgid=o.orgunitid
this query gives me multiple values(multiple rows) for the new parent column.
But the above code gives me the following error:
Cannot insert the value NULL into column 'acadid', table
'tempdb.dbo.table1______________________________________________________________000000014F2B'; column does not allow nulls. INSERT fails. The statement has been
terminated.
How can I fix it?
The reason why you have this error message is that you added a new column to your table1 table with the alter table statement, but instead of updating the value of this column for existing rows, you are adding new rows with only this column filled with an insert statement, and there may be a constraint (a primary key constraint?) on another column that does not allow nulls.
Instead of that, you probably want to update the value of this new column for the existing rows with an update like:
update table1
set table1.new_parent = table2.parent
from table1 inner join table2 on table2.orgid=table1.orgunitid
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 ;