简体   繁体   English

根据另一个表的值更新一个表的列值

[英]Update column value of one table based on values from another table

If I have two tables...如果我有两张桌子...

        table_x                          table_y
| location | latitude |  | location | min_latitude | max_latitude |
| -------- | -------- |  | -------- | ------------ | ------------ |
|          |   41.5   |  |  point_x |     41.0     |     42.0     |

How do I set table_x.location to table_y.location if table_x.latitude is in between table_y.min_latitude and table_y.max_latitude ?如何设置table_x.locationtable_y.location如果table_x.latitude是介于两者之间table_y.min_latitudetable_y.max_latitude I tried the code below but it cannot recognize table_y .我尝试了下面的代码,但它无法识别table_y

UPDATE table_x
SET table_x.location = table_y.location
WHERE table_x.latitude BETWEEN table_y.min_latitude AND table_y.max_latitude

In your query, table_y comes from nowhere.在您的查询中, table_y 无处不在。 Try this:尝试这个:

UPDATE table_x x
JOIN table_y y
ON x.latitude BETWEEN y.min_latitude AND y.max_latitude
SET x.location = y.location

you need to join the second table你需要加入第二张桌子

But as many rows can be selected for update, you should test this prior of running the query但是由于可以选择多行进行更新,您应该在运行查询之前对此进行测试

UPDATE table_x,table_y
SET table_x.location = table_y.location
WHERE table_x.latitude BETWEEN table_y.min_latitude AND table_y.max_latitude

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

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