I have two tables:-
Source Table: ACT_DT with columns (CUST_NAME, ACC_TYPE, CUST_STAT, SB_ACT_DT);
Target Table: ORG_DT with columns (CUST_NAME, ACC_TYPE, CUST_STAT, SB_ACT_DT);
The column SB_ACT_DT in the Target table has all null values. I need to update that column with the values of same column as in source table. The condition to be checked are:
ACC_TYPE='Billing' and CUST_STAT='Active'.
The Target table has to be updated only if the above conditions are found true.
How can I do it? Your help is appreciated.
What you are looking for is Update table using Join
UPDATE ORG_DT o
JOIN ACT_DT a
ON o.CUST_NAME=a.CUST_NAME
AND
o.ACC_TYPE=a.ACC_TYPE
AND
o.CUST_STAT=a.CUST_STAT
SET o.SB_ACT_DT = a.SB_ACT_DT
WHERE a.ACC_TYPE='Billing' AND a.CUST_STAT='Active'
Related
I have an existing table of products, and I'm trying to insert new attributes from other tables into my main product table.
Here's my query:
INSERT INTO company_attributes (amp)
SELECT company_attr_amperage.amp
FROM company_attr_amperage
INNER JOIN company_attributes
ON company_attr_amperage.product_id = company_attributes.product_id;
The error that I get is: Field 'product_id' doesn't have a default value.
I'm not trying to insert into the product_id column, I'm trying to insert into the amp column (as specified on row 1 of the query)
The amp column exists on the company_attributes table, but right now, every value is NULL
Thanks
You may just want to update the value in existing rows. If so:
UPDATE company_attributes ca JOIN
company_attr_amperage caa
ON caa.product_id = ca.product_id
SET ca.amp = caa.amp;
INSERT's job is to put new rows into a table, in your case company_attributes. Those new rows must be valid. Your table definition says that there's no default value for a column called product_id in that table. So, MySQL doesn't know what to put into that column when you don't provide it with your INSERT operation.
So, when you insert a row you must provide a value for that column.
There's no way to put a value into a column without creating a row for it, or UPDATEing an existing row.
I have two tables
Table 1 Schema:
Table1:: Product_Report Column:: PId, PReportName, PReportSize
And Second Table Schema:
Table2:: Product Column:: PId, PName, PSize, PCategory
In both table PId is PrimaryKey. I want to add PReportCategory as new column in Product_Report (Table1) in that value of PReportCategory is selected from PCategory of Product (Table2)
Something like:
INSERT into Product_Report (PReportCategory) VALUES Select PCategory from Product where (I guess here need help)
If I am right to path I am following then I guess in where condition I need help if not then please help me to construct query
Thank You
Q: "I want to add PReportCategory as new column in Product_Report"
ALTER TABLE Product_Report
ADD PReportCategory BIGINT DEFAULT NULL COMMENT 'ref Product.PCategory'
(Use an appropriate datataype in place of BIGINT, I added that as a placeholder datatype for the column.)
Q: "... value of PReportCategory is selected from PCategory of Product"
UPDATE Product_Report t
JOIN Product s
ON s.PId = t.PId
SET t.PReportCategory = s.PCategory
This would leave NULL values for PReportCategory in rows in Product_Report that don't have a matching row found in Product.
It's not at all clear what problem this is attempting to solve, i.e. why it's necessary to add this column and populate it. But this example of MySQL syntax demonstrates how we would 1) add a column to a table and 2) populate that column from values in rows in another table, matched based on the values of PId
I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.
How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.
Part 1:
In MySQL suppose I have Table A which has more columns than Table B. I want to transfer values from Table B to Table A where the id row in A matches the id Row in B and update the values in table A from the values in table B.
Part 2:
Table B is a superset of table A, so how does one insert ids and their corresponding values from table B into table A while also updating id's that are in table A.
Like FreshPrinceOfSO already mentioned in the comments, you won't get code for free here.
But here are at least the steps. Two possibilities. Either you split the work up in two statements, one update then one insert statement. Or you could work with
INSERT ... ON DUPLICATE KEY UPDATE ...
You would have to have an unique index on the table for this to work.
For the first solution mentioned you'd inner join the tables for the update first, that's trivial. Then for the insert you'd use a select with a left join and with is null checking for entries that are not already in the table.
Good luck...