简体   繁体   English

如何用其他表中的不同值更新多行?

[英]How to update multiple lines with different values from other tables?

I have 3 tables, two of them contain date, and the last one is trying to bring data from those tables. 我有3个表,其中两个包含日期,最后一个表试图从这些表中获取数据。 But, two (if we don't count the id column) of its columns contain data, that only this table has. 但是,其中两个列(如果不计算id列)包含数据,只有此表具有。

So, what I'm trying to do, is to import data from the other tables by doing multiple updates, one column at a time. 因此,我想做的是通过一次多次更新来从其他表中导入数据。

The tables have the following structure : 这些表具有以下结构:

test1(
  id_test1 serial NOT NULL,
  typeactemariage character varying(200) NOT NULL,
  datemariage character varying(200),
  id_lieumariage integer,
  id_conjoint integer,
  id_beaupere integer,
  id_bellemere integer,
  CONSTRAINT test1_pkey PRIMARY KEY (id_test1)
)

test2
(
  id_test2 serial NOT NULL,
  ville character varying(200),
  departement character varying(200) NOT NULL,
  CONSTRAINT test2_pkey PRIMARY KEY (id_test2)
)

test3
(
  id_test3 serial NOT NULL,
  typeacte character varying(100) NOT NULL,
  epoux character varying(100) NOT NULL,
  prenomepoux character varying(100),
  prenompereepoux character varying(100),
  nommereepoux character varying(100),
  prenommereepoux character varying(100),
  epouse character varying(100),
  prenomepouse character varying(100),
  prenompereepouse character varying(100),
  nommereepouse character varying(100),
  prenommereepouse character varying(100),
  lieu character varying(150) NOT NULL,
  dept character varying(100) NOT NULL,
  dates character varying(30),
  numvue character varying(100),
  CONSTRAINT test3_pkey PRIMARY KEY (id_test3)
)

test1 already has data in the columns typeactemariage and datemariage (and id_test1 ), and tries to bring the data from test2(id_test2) in the column (id_lieumariage) . test1在typeactemariagedatemariage (和id_test1 )列中已经有数据,并尝试将来自test2(id_test2)的数据引入(id_lieumariage)中 The other columns remain empty at the moment. 目前其他列仍为空。

This is what I wrote : 这是我写的:

UPDATE
    test1 
SET
    id_lieumariage = l.id_test2
FROM
    test1 m,
    test2 l,
    test3 o
WHERE
    o.id_test3 = m.id_test1
    AND o.lieu = l.ville
    AND o.dept = l.departement;

The code executes, the column has data, but it only fetches with the first value seen (at row 1), which is not what I want. 代码执行后,该列中有数据,但它仅使用看到的第一个值(在第1行)进行获取,这不是我想要的。 Is there any way to make it do a loop, to do this update on every single row? 有什么方法可以使其循环,以便在每一行上进行此更新?

Thanks ! 谢谢 !


EDIT : I'm putting pictures of my tables, and gonna try to explain better. 编辑:我正在放我的桌子的图片,并将尝试更好地解释。

test1 : http://puu.sh/scVCy/8479b7e4f2.png test1: http : //puu.sh/scVCy/8479b7e4f2.png

test2 : http://puu.sh/scVmL/901df2d74a.png test2: http : //puu.sh/scVmL/901df2d74a.png

test3 : http://puu.sh/scVoz/04be151c71.png test3: http : //puu.sh/scVoz/04be151c71.png

test1 after the execution of my code : http://puu.sh/scVpC/3209b5f4a6.png 我的代码执行后的test1: http : //puu.sh/scVpC/3209b5f4a6.png

The id_lieumariage column fills itself with the first value it gets, 1 . id_lieumariage列用它获得的第一个值1填充自身。 With my code, I was expecting to get the first row with the value 1, second row with value 2, third row with value 3. 使用我的代码,我期望第一行的值为1,第二行的值为2,第三行的值为3。

I have something like 100k+ of values that I'm gonna have to import (I only shew 3 rows here, to try to simplify my example). 我有大约100k +的值需要导入(我这里只显示了3行,以简化示例)。

So, what I'd need, is to be able to get the different id_lieumariage values for each row. 因此,我需要的是能够为每一行获取不同的id_lieumariage值。 Tell me if you didn't understand my explanation, I don't have a very good English. 告诉我,如果您不理解我的解释,我的英语说得不好。


EDIT 2 : I'm gonna provide the data I have : 编辑2:我将提供我拥有的数据:

CREATE TABLE test1()
ALTER TABLE test1 ADD COLUMN id_test1 SERIAL PRIMARY KEY, ADD COLUMN typeactemariage VARCHAR(200) NOT NULL, ADD COLUMN datemariage VARCHAR(200), ADD COLUMN id_lieumariage INTEGER, ADD COLUMN id_conjoint INTEGER, ADD COLUMN id_beaupere INTEGER, ADD COLUMN id_bellemere INTEGER;

CREATE TABLE test2()
ALTER TABLE test2 ADD COLUMN id_test2 SERIAL PRIMARY KEY, ADD COLUMN ville VARCHAR(200), ADD COLUMN departement VARCHAR(200) NOT NULL*;

CREATE TABLE test3()
ALTER TABLE test3 ADD COLUMN id_test3 SERIAL PRIMARY KEY, ADD COLUMN typeacte VARCHAR(200) NOT NULL, ADD COLUMN epoux VARCHAR(200), ADD COLUMN prenomepoux VARCHAR(200), ADD COLUMN prenompereepoux VARCHAR(200), ADD COLUMN nommereepoux VARCHAR(200), ADD COLUMN prenommereepoux VARCHAR(200), ADD COLUMN epouse VARCHAR(200), ADD COLUMN prenomepouse VARCHAR(200), ADD COLUMN nommereepouse VARCHAR(200), ADD COLUMN prenommereepouse VARCHAR(200), ADD COLUMN lieu VARCHAR(200), ADD COLUMN dept VARCHAR(200), ADD COLUMN dates VARCHAR(200), ADD COLUMN numvue VARCHAR(200);

INSERT INTO test1(typeactemariage, datemariage, id_lieumariage, id_conjoint, id_beaupere, id_bellemere)
VALUES ('Contrat de mariage', '21/11/2016', NULL, NULL, NULL, NULL),
('Contrat de mariage', '25/11/2016', NULL, NULL, NULL, NULL)
('Contrat de mariage', '11/11/2016', NULL, NULL, NULL, NULL);

INSERT INTO test2(ville, departement)
VALUES('Royan', '17'),
('Rochefort', '17'),
('Aytre', '17');

INSERT INTO test3(typeacte, epoux, prenomepoux, prenompereepoux, nommereepoux, prenommereepoux, epouse, prenomepouse, prenompereepouse, nommereepouse, prenommereepouse, lieu, dept, dates, numvue)
VALUES ('Contrat de mariage', 'DUPOND', 'Loris', 'Jacques', 'Duponne', 'Jeanne', 'BOURDOIN', 'Manon', 'Dujonni', 'Jinny', 'Royan', '17', '11/11/2016', '10/70'),
('Contrat de mariage', 'DUPONT', 'Pierre', 'Jerome', 'Dupuits', 'Jeannette', 'BOURLIAN', 'Philippine', 'Marpa', 'Elisa', 'Rochefort', '17', '21/11/2016', '10/20'),
('Contrat de mariage', 'Tintin', 'Milou', 'Haddock', 'Bond', 'Barack', 'Dutroux', 'Anna', 'Trane', 'Melissa', 'Aytre', '17', '25/11/2016', '10/25');

What I'm supposed supposed to have after the execution of my request is the following : 执行请求后,我应该拥有的内容如下:

row(1) of test1 : 1,Contrat de mariage, 21/11/2016, 1, NULL, NULL, NULL test1的第(1)行:1,Maritra对比,21/11/2016,1,NULL,NULL,NULL

row(2) of test1 : 1,Contrat de mariage, 25/11/2016, 2, NULL, NULL, NULL test1的第(2)行:1,Maritra对比,25/11/2016,2,NULL,NULL,NULL

row(3) of test1 : 1,Contrat de mariage, 11/11/2016, 3, NULL, NULL, NULL test1的第(3)行:1,Maritra对比,11/11/2016,3,NULL,NULL,NULL

What I do get : 我得到的是:

row(1) of test1 : 1,Contrat de mariage, 21/11/2016, 1, NULL, NULL, NULL test1的第(1)行:1,Maritra对比,21/11/2016,1,NULL,NULL,NULL

row(2) of test1 : 1,Contrat de mariage, 25/11/2016, 1, NULL, NULL, NULL test1的第(2)行:1,Maritra对比,25/11/2016,1,NULL,NULL,NULL

row(3) of test1 : 1,Contrat de mariage, 11/11/2016, 1, NULL, NULL, NULL test1的第(3)行:1,Maritra对比,11/11/2016,1,NULL,NULL,NULL

What my code is doing, is taking the first l.id_test2 it finds, and puts it on every single row. 我的代码正在做的事情是将找到的第一个l.id_test2放入每行中。 If I modify the request to have : 如果我将请求修改为:

UPDATE
    test1 
SET
    id_lieumariage = l.id_test2
FROM
    test1 m,
    test2 l,
    test3 o
WHERE
    o.id_test3 = m.id_test1
    AND o.lieu = l.ville
    AND o.dept = l.departement
    AND id_test1 = 2

Then, what I get is : 然后,我得到的是:

row(1) of test1 : 1,Contrat de mariage, 21/11/2016, 1, NULL, NULL, NULL test1的第(1)行:1,Maritra对比,21/11/2016,1,NULL,NULL,NULL

row(2) of test1 : 1,Contrat de mariage, 25/11/2016, 1, NULL, NULL, NULL test1的第(2)行:1,Maritra对比,25/11/2016,1,NULL,NULL,NULL

row(3) of test1 : 1,Contrat de mariage, 11/11/2016, 1, NULL, NULL, NULL test1的第(3)行:1,Maritra对比,11/11/2016,1,NULL,NULL,NULL

Tell me if you need more informations ! 告诉我您是否需要更多信息!

My answer before edit was for MySql, because MySql tag fooled my a little bit and I tested previous query on MySql database. 在编辑之前,我的答案是针对MySql,因为MySql标记欺骗了我一点,并且我在MySql数据库上测试了先前的查询。 I don't have option to test these queries for postgresql at this moment but maybe they will help you. 目前,我没有选择测试这些查询的Postgresql查询,但也许它们会为您提供帮助。

UPDATE
  test1
SET
  id_lieumariage = subquery.id_test2
FROM (
  SELECT id_test2, id_test1
  FROM test1 m, test2 l, test3 o
  WHERE o.id_test3 = m.id_test1
    AND o.lieu = l.ville
    AND o.dept = l.departement) AS subquery
WHERE test1.id_test1 = subquery.id_test1;

Before you'll execute update you can test with this additional query how many rows will be affected by update: 在执行更新之前,您可以使用此附加查询测试更新将影响多少行:

SELECT
  count(*)
FROM (
  SELECT id_test2, id_test1
  FROM test1 m, test2 l, test3 o
  WHERE o.id_test3 = m.id_test1
    AND o.lieu = l.ville
    AND o.dept = l.departement) AS subquery
WHERE test1.id_test1 = subquery.id_test1;

select Subquery should provide joined 3 tables with all requirements and the outer update query should lookup that 'subquery' table for appropriate values to set in test1 table. select子查询应提供符合所有要求的3个联接表,外部更新查询应在“子查询”表中查找在test1表中设置的适当值。

update test1 m
set id_lieumariage = l.id_test2
from
    test3 o
    inner join
    test2 l on o.lieu = l.ville and o.dept = l.departement
where o.id_test3 = m.id_test1
;

table test1;
 id_test1 |  typeactemariage   | datemariage | id_lieumariage | id_conjoint | id_beaupere | id_bellemere 
----------+--------------------+-------------+----------------+-------------+-------------+--------------
        1 | Contrat de mariage | 21/11/2016  |              1 |             |             |             
        2 | Contrat de mariage | 25/11/2016  |              2 |             |             |             
        3 | Contrat de mariage | 11/11/2016  |              3 |             |             |             

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM