I have an issue with a SQL query/SP where I'm trying to update a table that has missing data in specific fields from another table where the data in those same fields exists and is valid. The trick here is I would like to anchor on a value in the first table. I can make it work with a INSERT INTO / SELECT FROM combo, but it creates a duplicate record.
Im using mysql 5.x. Here are the details. The table with missing data is thisweek and the table with valid data is lastweek. Field 1 is MACAddress (which exists and is the anchor) and exists in both tables (for ex. BE:EF:BA:BE:CA:FE), Fields 2-10 in thisweek are blank (''), but there is data in those same fields(Fields2-10) in table lastweek.
UPDATE thisweek
SET thisweek.field2 = lastweek.field2
where thisweek.MACAddress = lastweek.MACAddress and thisweek.filed2 = '';
I know that query isn't anywhere close, so looking for help. Again, the same MACAddress exists in both tables, with the only difference between tables being that field2 in thisweek is blank (and it shouldn't be) and needs to be equal to lastweek.field2 for that MACAddress.
Thanks all.
I think you want the following:
UPDATE tw
SET tw.Field2 = lw.Field2
FROM
ThisWeek tw
JOIN LastWeek lw ON tw.MACAddress = lw.MACAddress
WHERE
tw.Field2 = ''
you need to JOIN thisweek and lastweek tables.
UPDATE thisweek
JOIN lastweek
ON thisweek.MACAddress = lastweek.MACAddress
AND thisweek.field2 =''
SET thisweek.field2 = lastweek.field2
update table1
inner join table2 on table1.id = table2.id and (table1.name = '' or table1.name is null)
set table1.name = table2.name;
Related
https://imgur.com/a/0443EiL
Above is the image of what I am working with.
I need to insert the Styletype column so that the StyleYype matches up with the StyleCode column.
seems you need update based on join
update table1
inner join table2 ON table2.style = table1.styleCode
set table1.styleType = table2,styleType
I am not sure how to ask this question - it might be answered somewhere, but it's kind of messy ;)
There are two tables that sort flags (labels) for objects somewhere else.
Table 1: the flags in booka_flags
columns of interest are id(INT) and title(VARCHAR)
Table 2: the flag items in booka_flags_items
columns of interest are id(INT) and flag(VARCHAR)
I want to change booka_flags_items.flag from VARCHAR (which is available from booka_flags.title) to ID (which is available from booka_flags.id)
My "idea" of writing this query (after some research) was the following:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = booka_flags.id
FROM
booka_flags_items
INNER JOIN
authors
ON
booka_flags_items.flag = booka_flags.title
but it does not work. Also not working is this query:
UPDATE
booka_flags_items
SET
booka_flags_items.flag = (
SELECT booka_flags.id
FROM booka_flags
INNER JOIN booka_flags_items
ON booka_flags_items.flag = booka_flags.title
)
What would be the right way to solve a query like that?
I'm guessing you would like to update id column of booka_flags with id column of booka_flags where title matches flags. Your updated query looks incorrect as per mysql
Try:
UPDATE booka_flags_items t1 INNER JOIN booka_flags t2 On t1.flag = t2.title
SET t1.id = t2.id
I have: something like
UPDATE table
SET field = (SELECT field FROM another_table WHERE id = #id);
Problem: SELECT field FROM another_table WHERE id = #id subquery can return one field or EMPTY SET.
Question: How to handle situation when subquery returns empty set?
Updated:
UPDATE table t
SET field = IF((SELECT field FROM another_table WHERE id = #id) IS NOT NULL, -- select field
(SELECT field FROM another_table WHERE id = #id), -- Problem #1: select field AGAIN!
(SELECT field FROM table WHERE id = t.id) -- Problem #2: try to not change value, so select the current field value!!
);
If function can be useful:
UPDATE table
SET field = if((SELECT field FROM another_table WHERE id = #id) IS NULL,true,false);
You can add the conditional:
WHERE (SELECT COUNT(*) FROM another_table WHERE id = #id) > 0
This will make sure that at least one row exists in another_table with the id. See my SQL Fiddle as an example.
Note: this may not be the most efficient because it does a count on another_table, and if it is greater than 1 it will do another SELECT (two sub-queries). Instead, you can do an INNER JOIN:
UPDATE table
INNER JOIN another_table ON table.id=another_table.id
SET table.field = another_table.field
WHERE another_table.id = #id;
See this SQL Fiddle. The reason why I saved this as a second option, is not all SQL languages can UPDATE with joins (MySQL can). Also, you need some way to relate the tables..in this case I said that the table.id we are updating is equal to another_table.id we are taking the data from.
NOTE The UPDATE statement will modify EVERY row in table and assign the same value to every row; that seems a little unusual.
To answer your question:
If you want to handle the "empty set" by not updating any rows in table, then one way to do this is with a JOIN to an inline view:
UPDATE table t
CROSS
JOIN (SELECT a.field
FROM another_table a
WHERE a.id = #id
LIMIT 1
) s
SET t.field = s.field
Note that if the inline view query (aliased as s) return an "empty set", then no rows in table will be updated, because the JOIN operation will also return an "empty set", meaning there are zero rows to be updated.
Modified some stuff from my pic so you guys can understand it
I have this database. I am trying to update a value from a table based on another value from an another table.
I want to update the SUM from salary like this :
( sum = presence * 5 )
This is what I've been trying to use ( unsuccessful )
update table salary
set suma.salary = users.presence * 5
FROM salary INNER JOIN users1 INNER JOIN presence on id_salary = id_presence
I am not sure what to do, I'd appreciate some help, Thanks
In MySQL to UPDATE tables with a join you use this syntax:
UPDATE table1, table2
SET table1.column = some expression
WHERE table1.column = table2.column
That said, even with the updated picture, in your SQL you are mentioning columns that I cannot understand in which table are to be found. You also have an inner join between salariu and users1, with no join condition. Could you please clean up the question and make everything clear?
Assuming you are making the updates to the db structure you were talking about, then you can start working on this one maybe:
UPDATE salary, presence
SET salary.sum = SUM(presence.hours) * 5
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
Another way, but I'm not sure it is supported in all RDBMS, would be something like this:
UPDATE salary
SET sum = (
SELECT SUM(presence.hours) * 5
FROM user, presence
WHERE presence.id = salary.id
AND <some filter on the month that depends on salary.date>
)
I am working with MySQL version 5. I have two tables structured as shown below.
Table1
From To Time TravelTime
Table2
From To Time CongestionTime
I would like to achieve the following output.
If From, To and Time are equal in both the tables, then assign TravelTime = CongestionTime
Table2 contains only a subset of the From | To | Time combinations available in Table1.
From is mysql reserved word. If you don't want to escape it, change column names "From" to "TimeFrom".
UPDATE table1,table2
SET table1.TravelTime=table2.CongestionTime
WHERE table1.From = table2.From
AND table1.To = table2.To
Update Table1 Set Table1.TravelTime = Table2.CongestionTime
FROM Table2
WHERE Table1.From = Table2.From
AND Table1.To = Table2.To
AND Table1.Time = Table2.Time
update table1
set traveltime = congestiontime
from table1
inner join table2 on table1.from = table2.from
and table1.to = table2.to
and table1.time = table2.time