TABLE 1
id name uf ibge
1 GOIANIA 'GO' null
2 BRASILIA 'DF' null
3 TOCANTINS 'TO' null
TABLE 2
id name uf ibge
1 GOI**Â**NIA 'GO' 5208707
2 BRAS**Í**LIA 'DF' 5300108
3 TOCANTINOPOLIS 'TO' 1721208
I need to update in table 1, the field ibge using some kind of like in the where clause to relate the description of the city with ibge code.
Could anyone help? Very grateful!
Try this...
UPDATE T1
SET T1.ibge = T2.ibge
FROM Table1 T1
INNER JOIN Table2 T2
ON T1.id = T2.id
Assuming you can join both tables on the uf field, like so:
UPDATE Table1
SET ibge = Table2.ibge
FROM
Table2
WHERE
Table1.uf = Table2.uf;
Fiddle here
UPDATE table1 tab1 SET ibge = (SELECT ibge FROM table2 tab2 WHERE tab1.name like (substring(tab2.name, 1, 3) || '%') LIMIT 1);
Assuming that the first 3 characters are the same in field name for table 1 and table 2
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
Can anyone help with the following ?
I have 2 tables and I need to update table#1 with data from table#2 like the example below
Table 1 :
ID - Name - Engine ID
1 - x - NULL
Table 2 :
ID - Name
0 - x
Result (in Table 1) :
ID - Name - Engine ID
1 - x - 0
using loop over table 1
Although you haven't tagged your database product, the syntax is almost same for many SQL backends:
update table1
set engineId = t2.Id
from table1 t1
inner join table2 t2 on t1.Name = t2.Name;
Here is DBFiddle Demo for SQL Server
EDIT: In case you are using MySQL its syntax is slightly peculiar:
update Table1 t1
inner join Table2 t2 on t1.Name = t2.Name
set t1.engineId = t2.Id;
In most databases, you could do this with a correlated subquery:
update t1
set engine_id = (select engine_id from t2 where t2.name = t1.name)
where engine_id is null
Note that for this to properly work, there should be no duplicate name in t2 (otherwise, the subquery would possibly return multiple rows, which would cause the query to error).
I have tables that are set up as shown below:
Table 1:
ID Name Date
1 a 2000-01-01
2 b 2001-01-01
3 c 2002-01-01`
Table 2:
ID Name
2 b
3 c
I would like to return all in Table 1 and then have a column that will hold a variable of either 'Yes' or 'No', based on whether they exist in Table 2, as shown below.
As shown:
Results:
Name Date Yes/No
a 2000-01-01 No
b 2001-01-01 Yes
c 2002-01-01 Yes
I have:
DECLARE #boolean as varchar(10)
IF EXISTS(
SELECT ID FROM Table 2
)
SET #boolean = 'Yes'
ELSE SET #boolean = 'No'
SELECT Name, Date, #boolean as 'Yes/No'
FROM Table 1
LEFT JOIN Table 2 u ON Table 1.ID = Table 2.ID
However, this returns the results as shown below:
Name Date Yes/No
a 2000-01-01 Yes
b 2001-01-01 Yes
c 2002-01-01 Yes
Any ideas on how to manipulate this query to return what is expected?
One option would be to LEFT JOIN the first table to the second one, and then check each record in the first table to see whether its ID matched anything in the second table.
SELECT t1.Name,
t1.Date,
CASE WHEN t2.ID IS NOT NULL THEN 'Yes' ELSE 'No' END AS [Yes/No]
FROM table1 t1
LEFT JOIN table2 t2
ON t1.ID = t2.ID
Demo here:
Rextester
By using LEFT JOIN and checking using CASE if there is no matching result on the second table (t2.ID IS NULL), you can easily get what you need as below:
SELECT Name, Date, CASE WHEN t2.ID IS NULL THEN 'No' ELSE 'Yes' END AS [Yes/No]
FROM Table1 t1 LEFT JOIN Table2 t2 ON t1.ID = t2.ID
If a left join might return multiple unwanted results you can use outer apply() instead:
select t1.*, [Yes/No]=coalesce(x.Yes,'No')
from table_1 t1
outer apply (
select Yes='Yes'
from table_2 t2
where t1.id = t2.id
) x
Select * , case when exists(select 1 from Table2 as b where b.ID = a.ID and b.Name = a.Name) then 'Yes' else 'No' end as 'Yes/No'
From table1 as a
Your query:
DECLARE #boolean as varchar(10)
IF EXISTS(
SELECT ID FROM Table 2
)
SET #boolean = 'Yes'
will always return true because you only check whether ID exists (which always exists records in table2), you forgot to check that should be the ID exists in table1, and you probably do not do in that way, because the indicator is a dynamic value.
I'm having difficulty with a SQL query.
I have two tables:
Table 1
ID/First Name
1 Ben
2 Barry
3 Birl
Table 2
ID/Full name
1 Ben Rurth
2 Barry Bird
3 Burney Saf
I want to run a check between the two tables where if the contents of the First Name in Table 1 is not in the Full name in table 2 the result will be returned, e.g. returning id 3, Birl, in the above example.
I have been trying queries like:
SELECT First_Name
from Table_1
WHERE NOT EXIST (SELECT Full_name from Table_2)
with no luck so far.
You can make use of LIKE clause combined with concatenation.
SELECT t1.First_Name,t2.Full_Name
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.Full_Name NOT LIKE '%' || t1.First_Name || '%'
Or
SELECT t1.First_Name,t2.Full_Name
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
WHERE t2.Full_Name NOT LIKE CONCAT('%', t1.First_Name, '%')
This is, understanding that both tables shares the ID column.
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