Inserting multiple values in a column - mysql

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

Related

Remove duplicate records in mysql

I have a table called leads with duplicate records
Leads:
*account_id
*campaign_id
I want to remove all the duplicate account_id where campaign_id equal to "51"
For example, if account_id = 1991 appears two times in the table then remove the one with campaign_id = "51" and keep the other one.
You could use a delete join:
DELETE t1
FROM yourTable t1
INNER JOIN yourTable t2
ON t2.account_id = t1.account_id AND
t2.campaign_id <> 51
WHERE
t1.campaign_id = 51;
There's no problem to delete from a table provided that:
You use the correct syntax.
You have done a backup of the table BEFORE you do any deleting.
However, I would suggest a different method:
Create a new table based on the existing table:
CREATE TABLE mytable_new LIKE mytable;
Add unique constraint (or PRIMARY KEY) on column(s) you don't want to have duplicates:
ALTER TABLE mytable_new ADD UNIQUE(column1,[column2]);
Note: if you want to identify a combination of two (or more) columns as unique, place all the column names in the UNIQUE() separated by comma. Maybe in your case, the constraint would be UNIQUE(account_id, campaign_id).
Insert data from original table to new table:
INSERT IGNORE INTO mytable_new SELECT * FROM mytable;
Note: the IGNORE will insert only non-duplicate values that match with the UNIQUE() constraint. If you have an app that runs a MySQL INSERT query to the table, you have to update the query by adding IGNORE.
Check data consistency and once you're satisfied, rename both tables:
RENAME TABLE mytable TO mytable_old;
RENAME TABLE mytable_new TO mytable;
The best thing about this is that in case that if you see anything wrong with the new table, you still have the original table.
Changing the name of the tables only take less than a second, the probable issue here is that it might take a while to do the INSERT IGNORE if you have a large data.
Demo fiddle
DELETE t1
FROM yourTable t1
INNER JOIN yourTable t2
ON t2.account_id = t1.account_id AND
t2.campaign_id <> 51
WHERE
t1.campaign_id = 51;

Data wont insert into table using insert into and right join

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.

How do I add the result of a SELECT as a new column in an existing table?

In SQL (whichever variant you like, say MySQL or MonetDB) it's very intuitive and straightforward to do:
CREATE TABLE t2 AS SELECT c1 FROM t1;
and get the selection result as a new column in a new table. But what if you want the result as a new column in the same table (table t1)? Let's assume c1 is of type INT and may be null. We would need to write:
ALTER TABLE t1 ADD c2 INT;
UPDATE TABLE t1 SET c2 = c1;
and that's the easy version, since if it's a non-null column we would need to initialize the new column with some value; and if the data comes from multiple tables I'd need some kind of inner query in the UPDATE (if it would be at all possible).
Can I select-into-a-column with a single command somehow?
You use join:
UPDATE t1 JOIN
t2
ON t2.? = t1.?
SET t1.c2 = t2.c1;
The ? is a placeholder for the column used to identity which row in t2 should update which row in t1.

MySQL Insert join table statement and update field on joined table

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.

my sql using insert and select together

Im trying to use insert and select in one query.Both the tables have same number of columns except for one column in the table where data is to be inserted
table2 is the mirror image of table1 except for a column called comments;
insert into table2 select * from table1 where city='XYZ' and name = 'STY'
since the no of columns arent equal i get the following error:
Column count doesn't match value count at row 1
INSERT INTO table2 (Coln1,Coln2,Coln3,....) SELECT * FROM table1 WHERE `city`='XYZ' AND `name`='STY';
For the extra column in the table2, NULL is set by default.