Replace column values in mySQL table with values from a second table - mysql

I have a table named "ONE" with a column "Names".
I have a second table called "TWO" with two columns : "Old_Names" and "New_Names".
I want to replace the "Names" in table "ONE" with the "Old_Names" from table "TWO".
The values in "Names" are the same as in "Old_Names" in my tables.
I am trying to do that but I get an error on mySQL :
update ONE set (ONE.Names=TWO.New_Names)
from ONE
join TWO on (ONE.Names=TWO.Old_Names);

Look at the update query :
update one o
join two t on ( o.names = t.old_names )
set O.names = t.new_names;
The join clause has to be write at the beginning of the query =>
http://sqlfiddle.com/#!9/9f292c

update ONE
join TWO on ONE.Names = TWO.Old_Names
set ONE.Names = TWO.New_Names

Related

fetch id from table1 based on some conditions and check if that id exists in other table

I have 2 tables; alternate identifier table and a master table
the above image represents the column from alternate identifiers table
and master table have these columns
Now, First I fetch company_group_id from alternate identifier table where bank_entity_id='somevalue' and id_value = 'somevalue';
if this query return nothing than end. if this query return company_group_id than I will check that company_group_id in master table where company_group_id = selected company group id.
after getting this if its present in the table than i will check hashcode if it presents in the master table.
separate queries are like this:
select company_group_id from aes_batch.aes_company_group_alternate_identifiers
WHERE ID_VALUE = '525' and BANK_ENTITY_ID='UOBS';
select company_group_id, hashcode from aes_batch.aes_company_group_master where company_group_id = 'value from the last query' and hashcode="value";
I want to combine these queries to get same result.
this is what I tried but failed.
SELECT t1.company_group_id
FROM aes_batch.aes_company_group_alternate_identifiers t1
LEFT JOIN aes_batch.aes_company_group_master t2
ON t1.company_group_id = t2.company_group_id
WHERE t2.company_group_id IS NULL;
SELECT *
FROM aes_batch.aes_company_group_master B
WHERE NOT EXISTS (SELECT 1
FROM aes_batch.aes_company_group_alternate_identifiers A
WHERE B.company_group_id = A.company_group_id);
Select a.company_group_id, b.company_group_id,
b.hashcode, a.id_value
from aes_batch.aes_company_group_alternate_identifiers a
LEFT JOIN aes_batch.aes_company_group_master b
ON b.company_group_id = a.company_group_id
WHERE a.id_value in ('524','525')
and a.bank_entity_id='UOBS';
can someone help me in this?

Ambiguous column name in update's where statement

I have one result table and two source tables. One source have same column name for key as result table, and second one have slightly different (same values, just different column name).
So if i use code as:
--Works
Update new_table set new_column_1 = source_column_1
from new_table t0
Left join source_table_1 t1
On t0.key_column = t1.id_column
Where key_column = t0.key_column
--Don't
Update new_table set new_column_2 = source_column_2
from new_table t0
Left join source_table_2 t1
On t0.key_column = t1.key_column
Where key_column = t0.key_column
2nd example would give me "Ambiguous column name" error in Where statement. (1st key_column should belong to update destination, 2nd to update source)
I manage around that problem by wrapping source for update into subquery which would rename key column into something else, but i'm curious why that happens in a first place. How come that key names for join make effect on where clause?
You must use the alias since both table have a column named key_column.
This query should work:
Update t0 set new_column_2 = t1.source_column_2 -- or t0.source_column_2?
from new_table t0
Left join source_table_2 t1
On t0.key_column = t1.key_column
Where t1.key_column = t0.key_column
The alias will replace the table name between update and set.
But the where clause does not seem necessary. It is identical to the ON clause. A INNER JOIN would be more appropriate here.

Update loanbook by reconciling loan stop dates and staff numbers

I have two tables with existing data pulled in from Excel spreadsheets converted to .csv:
table one : loanbook
table two : olb
staffno and loanstart columns are similar in both tables. For every staffno there can be multiple results in olb table.
PROBLEM:
I need to update loanstop column in table one (loanbook) with loanstop values from table two (olb) where staffno and loanstart are the same.
UPDATE loanbook3
SET loanbook3.loanstop = (
SELECT loanstop
FROM olb
WHERE olb.staffno = loanbook3.staffno
AND
olb.loanstart = loanbook3.loanstart
);
RESULT
#1242 - Subquery returns more than 1 row.
What do I do?
use limit 1 with your Subquery
UPDATE loanbook3 SET loanbook3.loanstop =
( SELECT loanstop FROM olb WHERE
olb.staffno = loanbook3.staffno
AND olb.loanstart = loanbook3.loanstart limit 1)

mySQL INSERT query with condition

I have two table: employee and privremeno and the both of them contains column jmbg. I want insert in employee (two columns) data from privremeno (two columns) so that data would be inserted in row where jmbg in employee is equal (the same) to jmbg in privremeno.
Something like:
INSERT INTO vg_pka.employee (stazDani, godZivota) select ukstaz, gz from
vg_pka.privremeno where vg_pka.privremeno.jmbgl = vg_pka.employee.jmbg;
How to do that?
you have to use update to inser values to existing table . I cant get your correct requirement . But the below is the syntax you have to use.
update table_name set = 'value' where (your condition)
You can use a join:
INSERT INTO
vg_pka.employee (stazDani, godZivota)
SELECT
ukstaz, gz
FROM
vg_pka.privremeno p
INNER JOIN
vg_pka.employee e
ON
p.jmbgl = e.jmbg;

Some doubts about this simple INNER JOIN query?

I have the following doubt about this simple INNER JOIN query.
I have these two tables that have to be joined togheter:
The first table is named VulnerabilityFix and contains the following columns:
Id: int identity
FixName: varchar
Vendor: varchar
Title: varchar
Version: varchar
The second table is named VulnerabilityAlertDocument_VulnerabilityFix (this bind the previous table to another table, but this is not important at this time) and contains the following columns:
VulnerabilityAlertDocumentId: int
VulnerabilityFixId: int
Now, on my DB the VulnerabilityFix table contains only an empty record (this record have an id but all the other fields are empty\null), infact if I perform a select *, I obtain:
select * from VulnerabilityFix
Id FixName Vendor Title Version
1
Into the VulnerabilityAlertDocument_VulnerabilityFix I have something like this:
select * from VulnerabilityAlertDocument_VulnerabilityFix
VulnerabilityAlertDocumentId VulnerabilityFixId
78385 1
78386 1
....................................................
....................................................
....................................................
78398 1
Ok, so I want JOIN toghert these 2 table in in such a way that passing the value of the VulnerabilityAlertDocumentId field of the VulnerabilityAlertDocument_VulnerabilityFix table, I obtain all the related record in the VulnerabilityFix table.
So in this case I aspect to retrieve the previous only record that having an id (having a value equal to 1) and all the other fields are empty\null.
So my query is:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityAlertDocumentId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 1
The problem is that when I execute this query I obtain an empty set of records and not the unique record that I expetc to obtain.
Why? What am I missing?
Tnx
I think your query should be more like:
SELECT VF.* FROM VulnerabilityAlertDocument_VulnerabilityFix VAD_VF
INNER JOIN VulnerabilityFix VF ON VAD_VF.VulnerabilityFixId = VF.Id
WHERE VAD_VF.VulnerabilityAlertDocumentId = 78385
That is, you are using the wrong column at your ON condition since VulnerabilityFixId seems to be the foreign key over VulnerabilityFix.Id and not VulnerabilityAlertDocumentId.
On the other hand, I can't see any VulnerabilityAlertDocument_VulnerabilityFix.VulnerabilityAlertDocumentId with value 1 in you data set (where condition)