SQL - update table from another table - syntax error - sql

I have two SQL tables:
matches (columns are hometeam, awayteam, id, gameweek)
teams (columns are teamcode, teamname)
matches.hometeam and matches.awayteam consist of integers that correspond to integers in teams.teamcode. I am trying to get matches.hometeam and matches.awayteam to update to strings that are taken from corresponding strings in teams.teamname. If that is impossible, then I need to create a new table as described.
I have tried the below code, but it produces a syntax error on the penultimate two lines (error 1064 (42000)). I can't figure out why.
UPDATE matches
SET matches.hometeam = teams.teamname
FROM matches
INNER JOIN teams
ON (matches.hometeam = teams.teamcode);

Error 1064 is a MySQL error. If you are using MySQL, the correct syntax is:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeam = t.teamname;
However, this will not really work. What you need to do is add ids:
alter table matches add hometeamcode int;
And then do:
UPDATE matches m JOIN
teams t
ON m.hometeam = t.teamcode
SET m.hometeamcode = t.teamname;
EDIT:
I think I misunderstood the whole situation. Your data model is totally correct. The matches table should have the integer codes, referring to the rows in teams.
You just need to write your query to get the names:
select m.*, th.teamname as hometeamname, ta.teamname as awayteamname
from matches m join
team th
on m.hometeam = th.teamcode join
team ta
on a.hometeam = ta.teamcode;
If you don't want to do the join, then encapsulate the logic in the view.

Related

Updating column in table using left join from another table in SQL

I want to update column 'location_cost' under 'animals' table basis cost stated under 'location_costs' table. Primary key is location to join both tables. I have tried the following code but it gives me syntax error.
//UPDATE animals.*
SET animals.location_cost = location_costs.costs
FROM animals
LEFT JOIN location_costs
ON animals.location = location_costs.location;//
Error: syntax error at or near "SET"
SET animals.location_cost = location_costs.costs
I am attaching a picture which gives an idea for the tables and columns here:
Tables View
I am not able to decipher the error and would appreciate if someone can help me with this code.
Thank you.
If you want to update only the rows in animals that have a matching location in location_costs then use this syntax:
UPDATE animals
SET location_cost = location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location;
If you want to update all the rows of animals (even the rows without a a matching location in location_costs will be updated to null), then use a correlated subquery:
UPDATE animals
SET location_cost = (
SELECT location_costs.costs
FROM location_costs
WHERE location_costs.location = animals.location
);
If animalid is a column with unique values, try using an alias as follows:
Update animals
Set location_cost = location_costs.costs
From animals As a Left Join location_costs On (a.location = location_costs.location)
Where animals.animalid = a.animalid;

Update column in table with data from another column using Join

I am trying to update data from one column to another using a join statement in SQL.
My two tables are rosters and scores. They share playerid.
I am trying to add data from scores.opp to rosters.opp.
Not sure what I am doing wrong. When I do the select statement below I am able to see the shared playerid, an empty column in rosters, and the column in scores that has the data I am looking to add to rosters.
SELECT a.playerid, a.opp, b.opp
FROM rosters a
JOIN scores b ON a.playerid = b.playerid
When I proceed to do the update I get an error. Here is what my update statement looks like:
UPDATE a
SET a.opp = b.opp
FROM rosters a
JOIN scores b ON a.playerid = b.playerid
I get an error saying, "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'FROM rosters a
JOIN scores b ON a.playerid = b.playerid' at line 3"
Any help would be appreciated. Thanks!
You can use the standard sql update with exists as follows:
UPDATE ROSTERS A
SET A.OPP = (
SELECT B.OPP FROM SCORES B
WHERE A.PLAYERID = B.PLAYERID)
WHERE EXISTS (
SELECT 1 FROM SCORES B
WHERE A.PLAYERID = B.PLAYERID)
Please make sure that there is only one record in the SCORES table for matching PLAYERID. if there are multiple records available in the SCORES table for a single PLAYERID then you need to use the aggregate function MAX, COUNT, etc.. in SELECT sub-query (like SELECT COUNT(B.OPP) FROM SCORES B -- or MAX)

Alternate solution for the query - Used INTERSECT function in oracle plsql

I am working on the query. I have two tables one is detail table where not grouping happen and its like including all the values and other table is line table which has important column grouped together from detail table.
I want to show all the column from line table and some column from detail table.
I am using below query to fetch my records
SELECT ab.*,
cd.phone_number,
cd.id
FROM xxx_line ab,
xxx_detail cd
WHERE cd.reference_number = ab.reference_number
AND cd.org_id = ab.org_id
AND cd.request_id = ab.request_id
AND ab.request_id = 13414224
INTERSECT
SELECT ab.*,
cd.phone_number,
cd.id
FROM xxx_line ab,
xxx_detail cd
WHERE cd.reference_number = ab.reference_number
AND cd.org_id = ab.org_id
AND cd.request_id = ab.request_id
AND ab.request_id = 13414224
The query is working fine...
But I want to know is there any other way for I can achieve the same result by not even using Intersect.
I purpose is to find out all possible way to get the same output.
The INTERSECT operator returns the unique set of rows returned by each query. The code can be re-written with a DISTINCT operator to make the meaning clearer:
SELECT DISTINCT
xxx_line.*,
xxx_detail.phone_number,
xxx_detail.id
FROM xxx_line
JOIN xxx_detail
ON xxx_line.reference_number = xxx_detail.reference_number
AND xxx_line.org_id = xxx_detail.org_id
AND xxx_line.request_id = xxx_detail.request_id
WHERE xxx_line.request_id = 13414224
I also replaced the old-fashioned join syntax with the newer ANSI join syntax (which makes relationships clearer by forcing the join tables and conditions to be listed close to each other) and removed the meaningless table aliases (because code complexity is more directly related to the number of variables than the number of characters).

Column in field list is ambiguous error

i've been recently working in mysql and in one of the requests i wrote :
SELECT SIGLE_EEP, ID_SOUS_MODULE, LIBELLE
FROM mef_edi.eep a, mef_edi.envoi e, mef_edi.sous_module s
WHERE a.ID_EEP = e.ID_EEP
AND a.ID_SOUS_MODULE = s.ID_SOUS_MODULE;
and they told me :
Column ID_SOUS_MODULE in field list is ambiguous
What should i do ?
More than one table has a column named ID_SOUS_MODULE.
So you need to name the table every time you mention the column to specify which table you mean.
Change
SELECT ID_SOUS_MODULE
for instance to
SELECT a.ID_SOUS_MODULE
I agree with the answer above, you may have duplicate column names across your 3 tables, assigning the table id (a, e, s) as noted above will avoid that issue in the select. In addition to what #juergen said you may want to get rid of that cartesian join by using an inner or left join (inner seems to be what your going for). The way you are joining your table you are joining every possible combination of rows together than filtering. using a proper join will get you better performance in the long run as your table line counts grow. Here is an example of a non cartesian join:
SELECT SIGLE_EEP, ID_SOUS_MODULE, LIBELLE
FROM mef_edi.eep a
INNER JOIN mef_edi.envoi e ON (a.ID_EEP = e.ID_EEP)
INNER JOIN mef_edi.sous_module s ON (a.ID_SOUS_MODULE = s.ID_SOUS_MODULE)

Why won't this merge statement work?

I've spent the better part of the day trying to determine why a merge statement won't work and I'm starting to think the problem must be something a bit exotic.
My database has dozens of PL/SQL procedures that use merge statements but I absolutely cannot get one in particular to work. Although it's much larger than the example shown, I've stripped it down so that it only updates a couple of columns and it still will not compile.
The error is 'ORA-00904 "alias"."column_name" invalid identifier'. This typically means that a column name was mistyped or, in the case of a merge, you are attempting to update a field that's used in a join. This is definately NOT the case. I've quadrupled-checked and the column names are right, they all exist and the format of the statement is exactly the same as what I'm using in many other place.
/**
Result: ORA-00904 "P"."SFDC_CUST_CONTACT_PK": invalid identifier
I'm certain that the table and column names are all correct.
If I join on any of the dozen or so other columns instead, I
get the exact same error.
Note: I'm NOT attempting to update the column that I join
against.
**/
merge into customer_contact c
using (select p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)
when matched then
update set
c.fax_number = p.fax_number,
c.email = p.email;
/***
This works fine on the same machine
**/
merge into customer_contact_legacy c
using (select ct.contact_legacy_pk,
ct.fax_number,
ct.email
from customer_contact_temp ct
) ct
on (upper(trim(ct.contact_legacy_pk)) = upper(trim(c.contact_legacy_pk)))
when matched then
update set
c.fax_number = ct.fax_number,
c.email = ct.email;
Any ideas what else could be wrong here? Could there be some type of corruption with the table?
The version is 10g.
It looks like your using clause is missing the column you're trying to join on.
Your code:
merge into customer_contact c
using (select p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)
Potential fix:
merge into customer_contact c
using (select p.sfdc_cust_contact_pk,
p.fax_number,
p.email
from sfdc_cust_contact_temp p
) p
on (p.sfdc_cust_contact_pk = c.sfdc_cust_contact_pk)