MySql使用嵌套选择插入动态查询

时间:2015-07-09 10:48:05

标签: mysql dynamic insert inner-join sql-insert

我尝试使用静态&混合的MySQL插入查询动态值。 INSERT命令是。

    SELECT transitionaction.TransitionID, transitionaction.ActionID
      FROM transitionaction
           INNER JOIN transition
              ON transitionaction.TransitionID = transition.TransitionID
     WHERE     transition.TenantID = 1
           AND transition.ProcessID = 1
           AND transition.CurrentStateID = 1
    ORDER BY transitionaction.TransitionID;

获取字段2&的查询3来自下面的查询。

def compute_rows(text, width):
    if len(text) < width:
        return 1
    phrases = text.replace('\r', '').split('\n')

    rows = 0
    for phrase in phrases:
        if len(phrase) < width:
            rows = rows + 1
        else:
            words = phrase.split(' ')
            temp = ''
            for idx, word in enumerate(words):
                temp = temp + word + ' '
                # check if column width exceeded
                if len(temp) > width:
                    rows = rows + 1
                    temp = '' + word + ' '
                # check if it is not the last word
                if idx == len(words) - 1 and len(temp) > 0:
                    rows = rows + 1
    return rows

我在这里做错了什么。 请指导我如何以最优化的方式实现这一目标。

1 个答案:

答案 0 :(得分:0)

您可以选择静态值作为查询的一部分,例如:

INSERT INTO ebdb.requestaction(RequestID, ActionID, TransactionID, IsActive, IsComplete)
SELECT 1, transitionaction.ActionID, transitionaction.TransitionID, 1, 0
  FROM transitionaction
       INNER JOIN transition
          ON transitionaction.TransitionID = transition.TransitionID
 WHERE     transition.TenantID = 1
       AND transition.ProcessID = 1
       AND transition.CurrentStateID = 1
ORDER BY transitionaction.TransitionID;

有关详细信息,请参阅MySQL's Insert...Select Syntax