SQL Update using data from other tables and foreign keys - sql

I am trying to update a column from table1 based off of data from two other tables.
Table 1 has columns id, columnIWantToUpdate, table2FK, table3FK
Table 2 has columns table2FK, table2_unique_id
Table 3 has columns table3FK, table3_unique_id
So I get table2_unique_id and table3_unique_id as inputs and I want to use the columns table2FK and table3FK to update table 1 based off of the unique_ids I received as input

One method uses filtering in the where clause:
update table1
set columnIWantToUpdate = ?
where exists (select 1
from table2 t2
where t2.table2FK = table1.table2FK
) or
exists (select 1
from table3 t3
where t3.table3FK = table1.table3FK
);
It is not clear if you want and and or for the conditions.

Related

Replacing the values of a column with Values of another column in a different Table without having an ID

I have 2 Tables.
1 table has my Data with a code.
The other table is a conversion table, where each code is assigned to a certain text.
I want that text to be in Table 1, my Data table.
Example:
i tried this:
You can use an inner select:
update table1 t1
set brandName = (select t2.text from table2 t2 where t1.key = t2.brand);

SQL INSERT from 1 table to another , WHERE existing columns in both tables share the same content

I have 2 tables,
Table 1 :columns = id, Category
Table 2 : columns = Category, CategoryID
I want to insert the 'id' from the Table 1 into the 'CategoryID' column in Table 2, where the contents of 'Category' in both tables are the same.
What you need is to update Table2 with the id from Table1 and you can do it with a correlated subquery:
UPDATE Table2
SET CategoryID = (SELECT id FROM Table1 WHERE Table1.Category = Table2.Category)
Depending on the database that you use, this can be done also with a join, but the syntax differs for each case.

ORACLE - Update a value from two different columns in differente tables with a filter

I'have a little question about a query.
I have to update a column from a table where there are only record of expense(integer).
I must increase the expense of 5% if the client is from a specific state, the column of the state is in a different table and the key in common is the address.
This is my query below :
UPDATE table 1 a
SET expense_vl = (
SELECT expense*1.05 FROM table 1
LEFT JOIN table2 b ON b.ADDRESS_ID=a.ADDRESS_ID
WHERE description_state IN 'lollyland'
)
I'd recommend using a semi-join:
update table_1 a
set expense_v1 = expense * 1.05
where exists (
select null
from table2 b
where
a.address_id = b.address_id and
b.description_state = 'lollyland'
)
Althought I must add that it would help if you include the DDL for your table. We're sort of guessing at which table "description" came from.
Also, when possible, include sample input for each table and desired output. We don't need a million records, just an example that illustrates your issue.
Try below
UPDATE table1 a SET expense_vl = (SELECT expense*1.05
FROM table2 b
WHERE b.ADDRESS_ID=a.ADDRESS_ID)
WHERE description_state IN 'lollyland'
Or try with subselect:
UPDATE table1
SET expense_vl = expense*1.05
WHERE ADDRESS_ID IN (SELECT ADDRESS_ID FROM table2 WHERE description_state IN 'lollyland')
I think you need to change your query like below :
UPDATE table 1 A
SET expense_vl=expense*1.05 FROM table 1
LEFT JOIN table2 B ON B.ADDRESS_ID=A.ADDRESS_ID
WHERE B.description_state IN 'lollyland'

How to delete rows from two tables which are in common with single query in postgresql

I have two tables t1 and t2
table t1 as follows:
id name
1 x
2 y
3 z
table t2 as follows:
id name
1 a
121 b
131 c
Here I am selecting rows that are common in both the tables i.e.,
SELECT *
from t1,t2
where t1.id=t2.id;
Now I want to delete the rows when id=1 in both the tables at once. I have tried to delete the rows but I am able to delete only in one table but not both. Can anyone help me out in solving this.
You can do that with a data modifying common table expression
with common_ids as (
select id
from t1
intersect
select id
from t2
), t1_delete as (
delete from t1
where id in (select id from common_ids)
)
delete from t2
where id in (select id from common_ids);
Online example: http://rextester.com/NAQ26877

Lookup two columns using SQL

I have a task. For Instance, I have 2 tables.
Table 1 columns ID, Name
Table 2 columns ID, Name
Data is like in Table 1:
ID Name
1 A
2 B
3 C
Data is like in Table 2:
ID Name
1 D
2 B
3 E
I want to write a SQL query which lookup two tables in both columns. I want to have the count, which records not matching (both columns) with table 2.
Here only one record was matched (2 B). So, I should get the count 2.
Thanks.
Use not exists to count the # of rows in table1 that are not in table2
select count(*) from mytable t1
where not exists (
select 1 from mytable t2
where t2.id = t1.id
and t2.name = t1.name
)