两个查询同时在它们之间共享数据

时间:2016-06-22 07:43:05

标签: mysql

我需要执行两个不同的mysql查询。主要问题是我需要第二个使用前一个数据,但不会干扰我选择数据。我很难解释,但这是我目前的综合查询:

SELECT accounts.id, username, password, proxy.ip, proxy.port
    FROM accounts
    LEFT JOIN proxy ON proxy.id = accounts.proxy_id
    ORDER BY RAND()
    LIMIT 1;

INSERT INTO task 
    (taskID, account_id, transaction_id) 
    VALUES ("*Insert Pregenerated Number*", accounts.id, *Insert Pregenerated Number*)

正如您在上一个查询中所看到的,我需要将accounts.ID视为我刚刚查询过的上一个帐户。但是我希望一次完成这项工作,并在第一次选择到我的程序时输出上述数据。任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:3)

为什么不只是INSERT .. SELECT

  "query": {
    "filtered": {
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "specialities",
                "query": {
                  "function_score": {
                    "score_mode": "sum",
                    "boost_mode": "sum",
                    "filter": {
                      "terms": {
                        "specialities.name.raw": ["Oncology"]
                      }
                    },
                    "functions": [
                      {
                        "field_value_factor": {
                          "field": "specialities.priority"
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  }

或将它们存储为变量:

INSERT INTO task 
    (taskID, account_id, transaction_id) 
SELECT "*Insert Pregenerated Number*",accounts.id,"*Insert Pregenerated Number*"
FROM accounts
LEFT JOIN proxy ON proxy.id = accounts.proxy_id
ORDER BY RAND()
LIMIT 1;

然后使用它们。

编辑:或者您可以创建一个包含此行的临时表,并根据您的喜好多次使用它:

  DECLARE myvar DOUBLE;
  ...
SELECT accounts.id, username, password, proxy.ip, proxy.port
INTO @myvar,@myvar2,....
FROM accounts
LEFT JOIN proxy ON proxy.id = accounts.proxy_id
ORDER BY RAND()
LIMIT 1;