复杂的sql更新

发布于 2024-12-09 03:15:43 字数 562 浏览 0 评论 0原文

表 MyTable 包含 4 列。

  • id、phone 列是唯一的。
  • ID、电话、邮政编码决不能为空。

id - phone -        salary   - zipcode

1  - 61730459987 - $56052.45 - 02456

2 - 21249620709 - NULL -      10023

3 - 21200830859 - $39089.28 - 10023

...

10000 - 64600830857 - $46063.72 - 03795**

我需要根据相同邮政编码的信息,通过将 NULL 替换为 salary_estimates 来删除 NULL。

对于上面的例子,#2 行的 NULL 可以替换为 $39089.28(我们从表中可以发现,另一个具有相同邮政编码 10023 的人赚了 $39089.28,因此可以将 #2 的 NULL 更新为 $39089.28)。 因此,在运行此查询后,空单元格的数量可以减少(尽管不会达到零)。

有人可以建议针对此问题进行更新查询吗?

The table MyTable contains 4 columns.

  • id, phone columns are unique.
  • id, phone, zipcode are never empty.

id - phone -        salary   - zipcode

1  - 61730459987 - $56052.45 - 02456

2 - 21249620709 - NULL -      10023

3 - 21200830859 - $39089.28 - 10023

...

10000 - 64600830857 - $46063.72 - 03795**

I need to remove NULLs by replacing them with salary_estimates, based on information from the same zip-codes.

As for the example above, NULL at row #2 could be replaced with $39089.28 (we can find from the table that another person with the same zip-code 10023 earns $39089.28, so it's OK to update NULL for #2 with $39089.28).
So, after we run this query, the number of empty cells could be reduced (although not up to zero).

Could anybody suggest an update query for this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-12-16 03:15:44
UPDATE
      MyTable AS tu
  JOIN 
      ( SELECT zipcode
             , AVG(salary) AS SalaryEstimate          --- AVG, MAX, MIN
                                                      --- your choice
        FROM MyTable 
        WHERE salary IS NOT NULL
        GROUP BY zipcode
      ) AS te
    ON te.zipcode = tu.zipcode
SET 
      tu.salary = te.SalaryEstimate
WHERE 
      tu.salary IS NULL
UPDATE
      MyTable AS tu
  JOIN 
      ( SELECT zipcode
             , AVG(salary) AS SalaryEstimate          --- AVG, MAX, MIN
                                                      --- your choice
        FROM MyTable 
        WHERE salary IS NOT NULL
        GROUP BY zipcode
      ) AS te
    ON te.zipcode = tu.zipcode
SET 
      tu.salary = te.SalaryEstimate
WHERE 
      tu.salary IS NULL
紫罗兰の梦幻 2024-12-16 03:15:44

我不确定它是否适用于 mysql。但在 MSSQL 中你可以这样做:

UPDATE MyTable
SET salary = (select top 1 salary from MyTable where zipcode = t1.zipcode and salary is not null)
FROM MyTable t1 
WHERE t1.salary is null

你需要检查 mysql 的等效连接情况。

参考号测试查询:

create table #temp (id int, salary decimal(18,2), zipcode varchar(10))
insert into #temp values (1,56052.45,02456)
insert into #temp values (2,NULL,1023)
insert into #temp values (3,39089.28,1023)
insert into #temp values (4,46063.72,03795)
insert into #temp values (5,NULL,02456)

UPDATE #temp
SET salary = (select top 1 salary from #temp where zipcode = t1.zipcode and salary is not null)
FROM #temp t1 
WHERE t1.salary is null

select * from #temp
drop table #temp 

I am not sure it will work for mysql. But in MSSQL you can do like this :

UPDATE MyTable
SET salary = (select top 1 salary from MyTable where zipcode = t1.zipcode and salary is not null)
FROM MyTable t1 
WHERE t1.salary is null

You need to check equivalent join case for mysql.

Ref. Test Query :

create table #temp (id int, salary decimal(18,2), zipcode varchar(10))
insert into #temp values (1,56052.45,02456)
insert into #temp values (2,NULL,1023)
insert into #temp values (3,39089.28,1023)
insert into #temp values (4,46063.72,03795)
insert into #temp values (5,NULL,02456)

UPDATE #temp
SET salary = (select top 1 salary from #temp where zipcode = t1.zipcode and salary is not null)
FROM #temp t1 
WHERE t1.salary is null

select * from #temp
drop table #temp 
闻呓 2024-12-16 03:15:44

您可以这样做:如果 salary 为空,它将替换为该邮政编码的工资。

isnull(salary,(Select top 1 salary  from  MyTable  where zipcode=10023 ))

You can do like this: if salary is null it will replace with salary of that zipcode.

isnull(salary,(Select top 1 salary  from  MyTable  where zipcode=10023 ))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文