简体   繁体   English

SQL更新左连在一起,其中where子句

[英]SQL Update Left Join with Where Clause

have a question about an SQL join. 对SQL连接有疑问。 I would like to update a table that runs within a program for a value calculation based on the price development of different accounts (eg price development of coal, leather, etc.). 我想更新一个程序中运行的表格,以便根据不同帐户的价格走势(例如煤炭,皮革等的价格走势)进行价值计算。 In the current version these are about 3000 accounts with an ID, a unique GP number and a value for each year. 在当前版本中,这些大约有3000个帐户,每个帐户具有ID,唯一的GP编号和每年的值。 The problem is that with the new release of the GP values about 900 accounts have been dropped. 问题在于,随着新发行的GP值,已经删除了大约900个帐户。 However, the program must be able to calculate further with these values, since the calculations were assigned to the IDs and not to the GP values. 但是,程序必须能够使用这些值进一步计算,因为计算是分配给ID而不是GP值。 I want to update the GP values for the years 2015, 2016 and 2017...However I don't know how to do it without shooting the IDs (values that are not updated and for which there are calculations will simply get the general price development, but this update can be neglected) 我想更新2015年,2016年和2017年的GP值...但是我不知道如何在不拍摄ID的情况下进行操作(未更新且经过计算的值只会得到一般价格开发,但可以忽略此更新)

So the first table looks like this: 因此,第一个表如下所示:

INSERT INTO `gps` (`id`, `Nummer`, `Beschreibung`, `Basisjahr`, `depricated`, `1949`, `1950`, `1951`, `1952`, `1953`, `1954`, `1955`, `1956`, `1957`, `1958`, `1959`, `1960`, `1961`, `1962`, `1963`, `1964`, `1965`, `1966`, `1967`, `1968`, `1969`, `1970`, `1971`, `1972`, `1973`, `1974`, `1975`, `1976`, `1977`, `1978`, `1979`, `1980`, `1981`, `1982`, `1983`, `1984`, `1985`, `1986`, `1987`, `1988`, `1989`, `1990`, `1991`, `1992`, `1993`, `1994`, `1995`, `1996`, `1997`, `1998`, `1999`, `2000`, `2001`, `2002`, `2003`, `2004`, `2005`, `2006`, `2007`, `2008`, `2009`, `2010`, `2011`, `2012`, `2013`, `2014`, `2015`, `2016`, `2017`, `2018`, `2019`, `created_at`, `updated_at`) VALUES 
(1, 'GP09-05', 'Kohle', 0, 0, '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '100.0', '109.9', '113.0', '114.1', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0000-00-00 00:00:00', '0000-00-00 00:00:00'), 
(2, 'GP09-06', 'Erdöl und Erdgas', 0, 0, '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '26.2', '27.8', '32.9', '31.0', '30.0', '45.5', '57.5', '52.0', '58.8', '55.0', '75.3', '96.4', '89.8', '113.9', '81.3', '100.0', '118.2', '142.2', '143.4', '0.0', '0.0', '0.0', '0.0', '0.0', '0.0', '0000-00-00 00:00:00', '0000-00-00 00:00:00'),

And so on (3000 datasets) 依此类推(3000个数据集)

The second table, which i wanna join, looks like this(database looks like this: "gps_neu.gps"): 我要加入的第二张表看起来像这样(数据库看起来像这样:“ gps_neu.gps”):

INSERT INTO `gps` (`Nummer`, `2014`, `2015`, `2016`,`2017`) VALUES 
('GP09-052010', '113', '111.3', '110.7','108'), 
('GP09-061010', '130.7', '79.5', '62', '81.1')

The DB looks like this: First: gps.gps.1949 and so on. DB看起来像这样:首先:gps.gps.1949,依此类推。 Second: gps_neu.gps.2014 and so on. 第二:gps_neu.gps.2014,依此类推。

My current code looks like this: 我当前的代码如下所示:

SELECT gps.2014,
       gps.2015,
       gps.2016,
       gps.2017
  FROM gps.gps
  JOIN LEFT gps_neu.gps.2014,
        gps_neu.gps.2015,
        gps_neu.gps.2016,
        gps_neu.gps.2017
    ON gps.gps.Nummer = gps_neu.gps.Nummer;

Hope someone can help me to fix it. 希望有人可以帮助我修复它。

After understanding your problem i think you don't need left join just need an update with inner join 了解了您的问题后,我认为您不需要左连接,只需要更新内部连接即可

update  gps.gps
  set gps.2014 = gps_neu.gps.2014 ,
   gps.2015 = gps_neu.gps.2014,
   gps.2016 = gps_neu.gps.2014,
   gps.2017 = gps_neu.gps.2014
FROM gps.gps
INNER JOIN gps_neu 
ON gps.gps.Nummer = gps_neu.gps.Nummer;

I think what you're looking for is IFNULL() in the SELECT list. 我认为您要查找的是SELECT列表中的IFNULL()

SELECT 
    IFNULL(gps.gps.2014, gps_neu.gps.2014) AS `2014`,
    IFNULL(gps.gps.2015, gps_neu.gps.2015) AS `2015`,
    IFNULL(gps.gps.2016, gps_neu.gps.2016) AS `2016`,
    IFNULL(gps.gps.2017, gps_neu.gps.2017) AS `2017`
FROM gps.gps
LEFT JOIN gps_neu.gps
ON gps.gps.Nummer = gps_neu.gps.Nummer;

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

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