简体   繁体   English

MySql更新,其中table_A.col_1在table_B.col_1和table_B.col_2之间

[英]MySql Update where table_A.col_1 Between table_B.col_1 and table_B.col_2

can you help me with an sql query? 你能帮我一个SQL查询吗?

I want to get a value from another table but cannot use an inner join because the "join" column is not exactly the same in these two tables. 我想从另一个表中获取一个值,但不能使用内部联接,因为这两个表中的“联接”列并不完全相同。 Instead I must check the place where "Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max" holds. 相反,我必须检查“ Table_A.clubZipCode在Table_B.zip_min和Table_B.zip_max之间的位置”所在的位置。

This is my research efford so far: 到目前为止,这是我的研究成果:

UPDATE
    Table_A
SET
    Table_A.clubState = Table_B.state
FROM
    clubs_data AS Table_A
    JOIN zip_to_state AS Table_B
WHERE
    Table_A.clubZipCode BETWEEN Table_B.zip_min AND Table_B.zip_max

However it draws an syntax-error on line 5. Thank you! 但是,它在第​​5行绘制了语法错误。谢谢!

I do not know what you want to achieve, but generally the update syntax using JOIN would be something like this: 我不知道您要实现什么,但是通常使用JOIN的更新语法如下:

UPDATE clubs_data AS Table_A

JOIN zip_to_state AS Table_B
    ON Table_A.clubZipCode >= Table_B.zip_min 
    AND Table_A.clubZipCode <= Table_B.zip_max

SET Table_A.clubState = Table_B.state;

EDIT : The solution just proposed by @Gab using a subquery might be better suited for you in this case. 编辑 :在这种情况下, 由@Gab提出的使用子查询的解决方案可能更适合您。

Here is your query re-written with a valid syntax: 这是使用有效语法重写的查询:

UPDATE clubs_data
SET clubState = (
                    SELECT state
                    FROM zip_to_state
                    WHERE clubs_data.clubZipCode >= zip_to_state.zip_min AND clubZipCode <= zip_to_state.zip_max
                    LIMIT 1
                );

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

相关问题 从 MYSQL 表 A 中返回行,其中 table_A.col_1 = table_b.col_2 不使用左外连接? - Return rows from MYSQL table A where table_A.col_1 = table_b.col_2 without using a left outer join? 选择*从表中的a和b之间的Col1,Col2 - SELECT * FROM Table WHERE Col1, Col2 BETWEEN a AND b 如何使用活动记录从col1 + col2 = b的表中选择* - how to select * from table where col1+col2=b using active record select from table where col_a in() ONLY where col_b value match - select from table where col_a in() ONLY where col_b value matches MySQL在“选择*从表中的(col,col)在”中没有利用主键 - Mysql not leveraging primary key in “select * from table where (col,col) in” 从具有b = a +“ s”的表中选择col1作为a,选择col1作为b? - Select col1 as a, col1 as b from table having b=a+“s”? mysql join-选择col1,其中col2匹配其他表中的(已过滤)col - mysql join - select col1 where col2 matches a (filtered) col in other table 一个表包含两列 a 和 b 如果 col a = col b 那么它将打印第一个值,否则它将打印两个值 - one table contains two column a and b if col a = col b then it will print first value otherwise it will print both values MySQL SELECT col,* FROM表 - MySQL SELECT col, * FROM table 将数据从表A插入到表B中(条件)并设置Col.a =“ xxx” - Insert Data from Table A to Table B WHERE (conditions) & SET Col.a = “xxx”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM