问题
I need to update one table's fields from another table's fields. I followed exactly the same link Stackoverflow but I am getting a Syntax Error.
Here is my query
UPDATE crm_leads
SET landlord_name = crm_owners.name,
last_name = crm_owners.last_name
FROM crm_owners
WHERE crm_owners.id =crm_leads.landlord_id
AND crm_leads.landlord_name = ''
AND crm_leads.last_name = ''
回答1:
Try placing an inner join after your UPDATE but before your SET. If I understand your schema correctly, something like this should work:
UPDATE crm_leads
INNER JOIN crm_owners
ON crm_owners.id = crm_leads.landlord_id
SET crm_leads.landlord_name = crm_owners.name,
crm_leads.last_name = crm_owners.last_name
WHERE crm_leads.landlord_name = ''
AND crm_leads.last_name = ''
回答2:
Two tables:
genes
: a list of gene names (with an additional column showing the approved gene name, for direct comparison). We will update this table.gene_synonyms
: a reference / loopk-up table of approved gene names, and their synonyms (aliases). We will use the data in this table to update the other table.
PSQL:
# SELECT * FROM genes ORDER BY id;
id name approved_name
-- ------- -------------
1 AIBP NAXE
2 ABG A1BG
3 CHP CHP1
4 CHP1 CHP1
5 SLCA1BP CHP1
6 NAXE NAXE
7 AIBP NAXE
8 APOA1BP NAXE
9 A1B A1BG
# SELECT * FROM gene_synonyms;
id approved_name synonym
-- ------------- -------
4 A1BG A1B
5 A1BG ABG
6 CHP1 CHP
7 CHP1 SLCA1BP
8 NAXE AIBP
9 NAXE APOA1BP
Update gene name in genes
table per approved name and synonyms in gene_synonyms
table:
# UPDATE genes
SET name = b.name
FROM gene_synonyms as b
WHERE NOT genes.name = b.name
AND genes.name = b.synonym;
UPDATE 7
# SELECT * from genes order by id;
id name approved_name
-- ---- -------------
1 NAXE NAXE
2 A1BG A1BG
3 CHP1 CHP1
4 CHP1 CHP1
5 CHP1 CHP1
6 NAXE NAXE
7 NAXE NAXE
8 NAXE NAXE
9 A1BG A1BG
# SELECT * from gene_synonyms;
id approved_name synonym
-- ------------- -------
4 A1BG A1B
5 A1BG ABG
6 CHP1 CHP
7 CHP1 SLCA1BP
8 NAXE AIBP
9 NAXE APOA1BP
This is based on @scott-bailey 's answer at SO 2763817; however, that answer is a better fit for this question (in my opinion).
来源:https://stackoverflow.com/questions/21657475/update-one-field-from-another-table