简体   繁体   English

根据MySQL语句中的另一个表列动态确定列值

[英]Determine column value dynamically based on another table column in MySQL statement

Is there a way to perform a query in MySQL like this one in pseudo code: 有没有一种方法可以在MySQL中像这样用伪代码执行查询:

INSERT INTO table_a(a, b, c) SELECT table_b.a, table_b.b, IF (table_b.c <100) 500 else 1000 WHERE ...

I know I can make two separate queries as a transaction but I want to know if there's a way to insert a value for a column based on another column from another table without going over the records twice. 我知道我可以在事务中进行两个单独的查询,但是我想知道是否有一种方法可以根据另一个表中的另一列为该列插入一个值,而无需重复两次记录。

Yes, there is. 就在这里。 You can use the CASE construct: 您可以使用CASE构造:

INSERT INTO table_a (a, b, c) 
SELECT table_b.a, table_b.b, CASE WHEN table_b.c < 100 THEN 500 else 1000 END
FROM   table_b
WHERE  ...

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

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