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;
Related
I'm try to update a column in table B with a value in table A where the passport_no match.
Below is my sql query.
update tabel_b
set b.country_id = a.national_id
from table_a a
join tabel_b b on b.passport_no = a.passport_no
where a.is_deleted = false;
On executing, I get ERROR: column "b" of relation "tabel_b" does not exist
How can I go about this please.
The immediate error is that you can't qualify the column you want to update. So it should be set country_id = ...
The bigger problem is however your join.
As documented in the manual
Do not repeat the target table as a from_item unless you intend a self-join
So the correct statement most probably should be:
update tabel_b
set country_id = a.national_id
from table_a a
where b.passport_no = a.passport_no
and a.is_deleted = false;
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)
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.
I've got two tables in Postgres:
Sources [id, term, type]
Posts [id, source_id, message, term, type]
I'm de-normalizing this data, so I'm adding the term and type columns to each of the posts, and getting rid of the Sources table.
Is there a way to do a FAST query that update the Posts with each of their respective sources data (there are about 8 million posts).
Something like:
UPDATE posts
JOIN sources
ON posts.source_id = sources.id
SET post.term = sources.term,
posts.term_type = sources.term_type;
But that is throwing a syntax error for me.
The correct syntax in Postgres is:
UPDATE posts
SET posts.source = sources.source,
post.term = sources.term,
posts.term_type = sources.term_type
FROM sources
WHERE posts.source_id = sources.id;
Or, you can use a row constructor:
UPDATE posts
SET (source, term, term_type) = (select s.source, s.term, s.term_type
from source s
where posts.source_id = s.id
);
In postgres each update consist on one insert and one delete. So beside double work also have impact if the index are active.
If you want update the whole table usually is much faster just create the table with the new values
CREATE TABLE post2 AS
SELECT p.id, p.source_id, p.message, s.term, s.term_type.
FROM posts p
INNER JOIN source s
ON p.source_id = s.id;
Then use ALTER to rename the tablename and create the proper index.
I am trying to update a table using the information that exist from another table. the table that I want to update is called YVDtemp and the second table I am getting the information from is called yvs_textsMain. I also use a 3rd table to match any records that I dont have and this table is called YVD. Here is my update statement.
update Yvdtemp
set
NAME = yvd_textsmain.[name]
SPIC = yvd_textsmain.[rname]
EFFECT = yvd_textsmain.[desc]
where exists (
select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null);
It seems that this statement is not working. It gives me an error "near 'spic': syntax error
but, when i use use the select statement apart from the update statement the select statement works and I get all the information that is not in YVD table and that information I want to pass it into YVDTemp table.
I am using SQLite.
As there is no matching condition between Yvdtemp and yvd_textsmain how will you map the rows in update statement.
Based on the requirement it looks like you are looking out for an Insert command which can be written as:
INSERT INTO Yvdtemp (NAME, SPIC, EFFECT)
Select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null
You miss the commas
NAME = yvd_textsmain.[name],
SPIC = yvd_textsmain.[rname],
EFFECT = yvd_textsmain.[desc]