S
S
sashavol2018-08-27 19:21:13
PostgreSQL
sashavol, 2018-08-27 19:21:13

UPDATE query with JOIN, how to update fields correctly?

Hello, I have very little experience with sql, and especially with join. But you need to make a request in the project to correct the data. It is hardly possible to do this pointwise, since you need to change this for 100 thousand records out of 700 thousand. Changes need to be made in table "A", and the user id is indicated in table "B" according to which you need to make changes in table "A", plus in the same table "A", take into account that the value is "200". Accordingly, in table "A" there is a linking id with table "B". It looks something like this:
Table A

id - уник. primary
id_Б - связывающий с таблицей Б id
rate - То поле, которое нужно изменить (int), если оно равно 200 (нужно поменять на 160)

Table B
id - уник. primary
id_client - поле, по которому нужно сделать выборку, например WHERE id_client == 2

Roughly I see the following request:
UPDATE А
SET rate = 160
WHERE rate = 200

// и тут надо соединить ещё конструкцию, уже из другой конструкции, чтобы проверить нужного юзера
FROM Б
WHERE A.id_Б = Б.id AND Б.id_client = 2

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
nozzy, 2018-08-27
@nozzy

Didn't check

update a t1
join b t2 on t2.id = t1.id_b
set t1.rate = 160
where t1.rate = 200
and t2.id_client = 2

A
Andrey K, 2018-08-28
@kuftachev

"Hello, very little experience with sql, and especially with join. But you need to make a query in the project to correct the data."
Either it's not true that this is in the project, or don't go there until you understand what you are doing!

A
arefanov, 2018-08-29
@arefanov

update a
set rate = 160
from b
where a.id = b.id and rate = 200
like so. also, the selection by b can be moved to a subquery/preselect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question