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
Related
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'm looking for a simple way to do an update on a table only if there is no other columns present in that same table with the same value I'm trying to update, ideally in a single query. So far I'm getting an error You specify target table 't1' for update in FROM clause. Here is what I tried in a few variations so far (still unable to get working):
UPDATE emailQueue AS t1
SET
t1.lockedOn = 1470053240
WHERE
(SELECT
COUNT(*)
FROM
emailQueue AS t2
WHERE
t2.lockedOn = 1470053240) = 0
AND t1.lockedOn IS NULL
In MySQL, you need to use a join. In this case, a left join is in order:
UPDATE emailQueue eq LEFT JOIN
emailQueue eq2
ON eq2.lockedOn = 1470053240
SET eq.lockedOn = 1470053240
WHERE eq.lockedOn IS NULL AND
eq2.lockedOn IS NULL;
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;
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 have two tables tb1 & tb2
I have to update a common column of both tables, i.e user_level
I have a common criteria for both tables like username.
So I want to update like this:
UPDATE tb1, tb2 SET user_level=1 WHERE username="Mr.X"
But somehow it is not working.
What would be the correct mysql query for this?
Try this:
UPDATE table_1 tb1,
table_2 tb2,
table_3 tb3
SET tb1.field2 = "some value",
tb2.field3 = "some other value",
tb3.field4 = "some another value"
WHERE tb1.field1 = tb2.field1
AND tb1.field1 = tb3.field1
AND tb1.field1 = "value"
I tested the code on MSAccess and SQL SERVER 2008
Your problem is solved,just follow this what I have done-
create table tb1(user_level int);
create table tb2(user_level int,username varchar(20));
insert into tb1 values(2);
insert into tb2 values(2,'Mr.X');
I have this two tables like this where user_level is common,now according to you I tried to update the user_level column in both table using one query on a common criteria for both table i.e. username.
I tried to update the value of user_level column from 2 to 3 in both tables where the username is 'Mr.X' using a single query,so I tried the following query and it perfectly worked..
update tb1 inner join tb2
on tb1.user_level=tb2.user_level
set tb1.user_level=3,
tb2.user_level=3
where tb2.username='Mr.X' ;
Try this:
update db1 inner join db2 on db1.username = db2.username
set db1.user_level = 1,
db2.user_level = 1
where db1.username = 'a';
See it here on fiddle: http://sqlfiddle.com/#!2/ba34ac/1
The correct query is that you have to specify the full table and row/column you are trying to update in the two tables and indeed database if you are updating across databases too.
typical example:
UPDATE tb1, tb2 SET tb1.username = '$username', tb2.location ='New York'WHERE tb1.userid = '$id' AND tb2.logid = '$logid'
We can update it without join like this
UPDATE table1 T1, table2 T2
SET T1.status = 1 ,T2.status = 1
WHERE T1.id = T2.tab1_id and T1.id = 1
We can update it with join like this
UPDATE table1
INNER join table2 on table1.id=table2.tab1_id
SET table1.status=3,table2.status=3
WHERE table1.id=1