Update on join in Sqlite - sql

I have two two tables A and B in which both P columns are common and I need to use update command in table B only when both p values are same and C column from table A is given
What I am trying is:
update B
set P =100
where B.P=A.P
and A.C=60
But it's giving me error no such column A.P

You are updating table B and do not have reference to table A, so sqlite just does not know where to look for. Try this:
UPDATE B
SET P = 100
WHERE B.P IN (SELECT A.P
FROM A
WHERE A.C = 60)

You can do it like this
Update B set P = 100 WHERE B.P = (Select P from A WHERE C = 60)

Related

Data Comparison between Two Tables

I have what should be simple (maybe) and I am just struggling with it.
Here is the scenario:
TABLE 1 contains all the data
TABLE 2 contains only a subset
I need a query that will look at table 1 and give a list of items that are not in table 2. Below is what I have but I know its not performing as such.
SELECT c.[DOC_ID], d.[DOCID]
FROM [dbo].[Custom_SUAM_Docuware] d
LEFT JOIN [dbo].[Custom_SUAM_Content] c ON (c.[DOC_ID] = d.[DOCID])
WHERE c.[DOC_ID] IS NULL
OR d.[DOCID] IS NULL
You are describing a not exists scenario.
You can't expect to return data from c since by definition what you want doesn't exist:
select d.DOCID
from dbo.Custom_SUAM_Docuware d
where not exists(
select * from dbo.Custom_SUAM_Content c
where c.DOC_ID = d.DOCID
);
you can use EXCEPT
SELECT c.[DOC_ID]
FROM [dbo].[Custom_SUAM_Content] c
EXCEPT
SELECT d.[DOC_ID]
FROM [dbo].[Custom_SUAM_Docuware] d ;
that would show all ids from c that are not in d

update multiple rows with joins

I have this query in postgresql:
select *
from A s
join B m on (s.id=m.id)
where m.key=4 and s.ran=some_input_from_user
This gives me all the rows that I need to update.
I want to set A.value to be 90 for all these rows.
It doesn't look like a standart update query
if I do...
Update A set value=90 where.....
then I can't do the join.
any ideas how to do it?
This is the basic update syntax for PostgreSQL where you are updating based on a join to another table:
update A s
set
value = 90
from B m
where
s.id = m.id and
m.key = 4 and
s.ran = some_input_from_user
The trick is you never use the alias in the lvalue for the set commands. In other words, value = 90 is not s.value = 90. It seems minor, but I'm pretty sure it will prevent your query from working. The rationale is if you are updating table A (alias s) then any fields you are updating are, de-facto, from table A -- no need to alias them, and to allow aliases would almost imply you could update something other than A with this statement, which you cannot.
You can definitely use them in the rvalues, so this would certainly be okay (if it were your desire to update A based on B):
update A s
set
value = m.salary * s.commission
from B m
where
s.id = m.id and
(s.value is null or
s.value != m.salary * s.commission)
Here is the query:
update a set value = 90
where exists (
select 1 from b
where a.id = b.id and b.key=4
and a.ran=some_input_from_user);
The above query will eliminate the requirement of reading table a twice.
Also you can use this query:
update a set value = 90
where a.id in
(select b.id from b
where a.id = b.id and b.key = 4
and a.ran=some_input_from_user);
TRY THIS
UPDATE A
SET A.VALUE = 90
from A
join B m on (A.id=m.id)
where m.key=4 and s.ran=some_input_from_user

UPDATE one table using another

I've buit a database in which there are these tables:
companies (a,b,nazov,ic_dph,mesto,ulica_cislo,psc)
finan (nazov,ic_dph,mesto,ulica_cislo,psc)
In table companies, the columns ic_dph,mesto,ulica and psc are empty. I want to update those columns with columns from finan table. I've tried many SQLite3 queries but none of them worked.
USING UPDATE:
UPDATE companies SET companies.ic_dph = finan.ic_dph, companies.mesto = finan.mesto, companies.ulica_cislo = finan.ulica_cislo, companies.psc=finan.psc WHERE companies.nazov = finan.nazov
USING JOIN:
CREATE TABLE new_table AS (SELECT * FROM companies JOIN finan)
Both queries returns some syntax errors.
Could you give me an advice what's wrong?
If you need to replace all the values, probably the best way is to delete rows and insert new values:
create temporary table t as
select c.a, c.b, c.nazov, f.ic_dph, f.mesto, f.ulica, f.psc
from companies c left join
finan f
on c.nazov = f.nazov;
delete from companies;
insert into companies(a, b, nazov, ic_dph, mesto, ulica, psc)
select a, b, nazov, ic_dph, mesto, ulica, psc
from t;
Do be careful using this method.
Another method is a bunch of correlated subqueries. First, be sure you have an index on finan(nazov). Then:
update companies c
set ic_dph = (select ic_ph from finan f where f.nazov = c.nazov),
mesto = (select mesto from finan f where f.nazov = c.nazov),
ulica = (select ulica from finan f where f.nazov = c.nazov),
psc = (select psc from finan f where f.nazov = c.nazov)
where exists (select 1 from finan f where f.nazov = c.nazov);

How to Update Using Join (Linked Server)

I want to match the data between 2 databases.
I have 2 Databases. Aa and Bb, and I want to compare Aa to Bb. Database Bb is in linked server
I have join code like this
SELECT
B.Employee_Name, B.Employee_NIP, B.DomainName, A.NAMA, A.NIP,
A.StatusEmployee, A.ActiveStatus
FROM
[SERVER-B].Bb.dbo.employee_hierar AS B RIGHT OUTER JOIN
Bb AS B ON B.NIP = A.Employee_NIP
and I want update A.StatusEmployee from Y to N if there is NULL data on B.Employee_Name and B.Employee_NIP
note:
SQLServer
Please Advice
You can use a join. Something like:
update a
set StatusEmployee = 'N'
from bb a LEFT JOIN
[SERVER-B].Bb.dbo.employee_hierar b
on B.NIP = A.Employee_NIP
where b.EmployeeName is null and b.Employee_NIP is null and
a.StatusEmployee = 'Y';
This is same to update multi tables in one database. Standard sql should be written like this:
UPDATE A
SET StatusEmployee = "N"
WHERE NOT EXISTS
( SELECT * FROM B WHERE B.NIP = A.Employee_NIP )

Updating a table

I have two tables: A, B.
Here's what I want to do (this is obviously not valid SQL):
UPDATE A a, B b SET a.pic = b.pic WHERE a.my_id = b.my_id
i.e. when the column my_id matches in tables A and B, I want to copy the pic column from B to A.
What's the right way of doing this?
The correct postgres query:
UPDATE A a
SET pic = b.pic
FROM B b
WHERE a.my_id = b.my_id;
You're only really updating A (and you can only update one table in an an UPDATE statement anyways)
UPDATE A a SET a.pic = ( SELECT b.pic FROM B b WHERE a.my_id = b.my_id)