I want update status column where id table 1 = id table 2 and status = ''
table1
===========================
id | delivery_status | name
===========================
table2
=============
id | status |
=============
delivery status on table1 and status on table2 are different case.
I just have one row match (id table 1 = id table 2) and 2 rows no match. if I query:
UPDATE myschema.table1
SET status = 'COMPLETED'
FROM myschema.table1 t1, myschema.table2 t2
WHERE t2.delivery_status = 'B' and t1.status = '' and t1.id = t2.id;
If this select, the result have 1 row, but when I execute this update query, 3 rows are update to COMPLETED. how to fix this problem? I just want to update one row. I already add the condition t1.id = t2.id. The id on table 1 and table 2 just match one row.
Only include the table to update once
UPDATE myschema.table1
SET status = 'COMPLETED'
FROM myschema.table2 t2
WHERE t2.delivery_status = 'B' and table1.status = '' and table1.id = t2.id;
Related
I have 2 tables :
Table1
parcel_number
pool_type
Table2
parcel_number
need_water
I would like to know if the table 1 "pool" column has a value than update the "need_water" column of the table 2 to yes.
Here what I would like to have for the table 2.
Table 1
parcel_number
pool_type
1
Circle
2
Oval
3
Null
4
Rectangular
Table 2
parcel_number
need_water
1
Yes
2
Yes
3
No
4
Yes
if exists(select a.pool_type
from table1 a
where a.parcel_number = b.parcel_number)
Begin
Update b
set b.need_water = 'Yes'
from table2 b
end
else
Begin
Update b
set b.need_water = 'No'
from table2 b
End
Thank you
A single update statement should be able to solve this:
UPDATE t2
SET t2.need_water = CASE WHEN t1.parcel_number IS NOT NULL THEN 'Yes' ELSE 'No' END
FROM table2 t2
INNER JOIN table1 t1
ON t2.parcel_number = t1.parcel_number
An UPDATE with a JOIN will work
UPDATE t2
SET need_water = 'yes'
FROM Table1 t1
JOIN Table2 t2
ON t1.parcel_number = t2.parcel_number
WHERE t1.pool_type IS NOT NULL
How to update 'date_from' (t1) using 'modfied' (t2) when it is like 20/07/20.
So in this case in t1 id's 1 and 2 are to be updated and id 3 stays.
Table 1:
id date_from
-----------------------
1 13/07/30
2 13/07/30
3 13/07/30
Table 2:
id name modified
-----------------------
1 x 20/07/20
2 y 20/07/20
3 z 19/05/10
Something like this:
update t1 a set
a.date_from = (select b.modified
from t2 b
where b.id = a.id
and b.modified = date '2020-07-20'
)
where exists (select null
from t2 c
where c.id = a.id
and c.modified = date '2020-07-20'
)
If speed matters then,
merge into t1 trg
using
(
select id, modified
from t2
where modified = date'2020-07-20'
) src
on ( trg.id = src.id )
when matched then update
set trg.date_from = src.modified
where lnnvl(trg.date_from = src.modified);
You know in advance which value needs to be assigned, so you just need to filter which rows should be updated. exists seems sufficient:
update t1
set date_from = date '2020-07-20'
where exists (
select 1 from t2 where t2.id = t1.id and t2.modified = date '2020-07-20'
)
cid ctypeid tid check asscid ci
19149 6 2 0 NULL 0
253440 1 1 0 22297922 1
1361285 5 2 0 NULL 1
22297922 2 1 1 NULL NULL
49821961 5 1 0 NULL 1
I have to check whether asscid i.e 22297922 is in the cid column which is there actually.
So I have to compare when the asscid is in the cid and then get the value of ci in case of asscid (where cid is 253440)which is 1 in this case and then assign the same value 1 of ci to cid column 22297922 which is null in this case.
Use an update with a self join.
Select:
SELECT t.asscid, t2.cid, t.ci, t2.ci --We will next update t2.ci with t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
Update:
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
Update with condition: (So that only rows that have different ci are updated)
UPDATE t2
SET t2.ci = t.ci
FROM table t
JOIN table t2 on t.asscid = t2.cid
and t.ci <> t2.ci
Select Assid from emp A
Where exists ( select 1 from Emp B where a.assid=b.Cis )
I have 2 tables as follows:
Table 1:
ID FName
1 Basics
2 Machine1
3 master
4 Machine2
15 Machine3
16 Machine16
Table 2:
ParentID Name InitialValue
1 Active 1
2 MachineName Entrylevel
2 Active 1
3 Active 1
4 MachineName Midlevellevel
4 Active 1
15 MachineName Endlevel
15 Active 1
16 MachineName Miscellenious
16 Active 0
Here, ID of Table 1 is referred as Parent ID at Table 2. I want "Initial Value" of Table 2 for MachineName Rows (of Table 2) provided "InitialValue" of Table 2 for Active Rows (of Table 2) are 1
Result should be like
ID InitialValue
2 Entrylevel
4 Midlevellevel
15 Endlevel
You could join the second table twice, once for MachineName, and once for Active:
SELECT t.ID, machine.InitialValue
FROM table1 t
INNER JOIN table2 machine
ON t.ID = machine.ParentId
AND machine.Name = 'MachineName'
INNER JOIN table2 active
ON t.ID = active.ParentId
AND active.Name = 'Active'
AND active.InitialValue = 1;
About Joins
The JOIN syntax allows you to link records to the previous table in your FROM list, most of the time via a relationship of foreign key - primary key. In a distant past, we used to do that with a WHERE condition, but that really is outdated syntax.
In the above query, that relationship of primary key - foreign key is expressed with t.ID = machine.ParentId in the first case. Note the alias that was defined for table2, so we can refer to it with machine.
Some extra condition(s) are added to the join condition, such as machine.Name = 'MachineName'. Those could just as well have been placed in a WHERE clause, but I like it this way.
Then the same table is joined again, this time with another alias. This time it filters the "Active" 1 records. Note that if the ID in table1 does not have a matching record with those conditions, that parent record will be excluded from the results.
So now we have the table1 records with a matching "MachineName" record and are sure there is an "Active" 1 record for it as well. This is what needs to be output.
Not sure if this is standard SQL but it should work using MySQL.
select T1.ID, T2.InitialValue
from Table1 T1 inner join Table2 T2 on T1.ID = T2.ParentId
where
T2.Name <> 'Active'
and exists (
select * from Table2 T3 where T3.ParentId = T1.ID and T3.Name = 'Active' and T3.InitialValue = 1
)
SELECT t1.ID, t2.InitialValue
FROM table1 t1 join table2 t2 on t1.ID=t2.ParentID
WHERE t2.name LIKE 'MachineName'AND t1.ID= ANY(SELECT t22.ParentID
FROM table2 t22
WHERE t22.InitialValue=1)
I think this should work
//slightly changed the condition in WHERE clausule (t2.parentID changed to t1.ID)
I have been searching for this scenario that has come across my desk, I have been searching reference sites but haven't had luck creating the correct SQL statement to complete this task.
Here is the PSEUDO code for the scenario.
UPDATE TABLE1
SET TABLE1.ID = TABLE1.From_ID,
TABLE1.VALUE = 'ALL'
WHERE TABLE1.From_ID = TABLE2.ID
AND TABLE2.NAME = 'TEST'
Basically I need to update two columns in TABLE1 only if the id from TABLE1 matches the ID's in the TABLE2 and the description column in TABLE2 equals to a string value the caveat is that TABL1 columns can't be change only if there is a correlation between the ID's from TABLE1 and TABLE2 and in TABLE2 that ID correlates to description column for a specific string value. Below is table structure and end result I'm trying to get too.
TABLE1:
FIELD_ID CONDITIONAL_VALUE FROM_FIELDID
--------------------------------------------
1 TEST 3
7 TEST 4
5 ANY 7
TABLE2:
FIELD_ID Description
----------------------------------------------
3 BLUE
4 BLUE
7 RED
In Transact-SQL (SQL Server's dialect of SQL), you need a FROM clause in your SQL if you specify more than the table you're trying to update.
update
TABLE1.ID
set
TABLE1.ID = TABLE1.From_ID ,
TABLE1.VALUE = 'ALL'
from
TABLE1,
TABLE2
where
TABLE1.From_ID = TABLE2.ID
AND TABLE2.NAME = ''TEST
You need to join data from TABLE1 to TABLE2
UPDATE t1
SET t1.ID = t1.From_ID
,t1.VALUE = 'ALL'
FROM Table1 AS t1
JOIN table2 AS t2
ON t1.From_ID = t2.ID
AND t2.NAME = 'TEST't1