合并两个不同的 select 和更新语句

Combine two different select and update statements

我有一个 select 查询和一个更新查询,我想将它们结合起来。

select 查询如下:

select questionDesc from myTable
where 
  questionId >= (
    select currentQuestionid from userTable 
    where userId='1'
  )
  and questionId <= (
    select currentQuestionid+4 from userTable 
    where userId='1'
  )

对于 user=1,此查询尝试从 myTable 中获取所有问题描述,其 questionId 位于 currentQuestionidcurrentQuestionid+4 之间(currentQuestionid 是特定于 userTable 中的用户的列。我稍后会在我的前端使用这个结果。

现在,我想将 currentQuesionid 更新为 currentQuestionid+5。这可以通过以下方式实现:

UPDATE userTable SET currentQuesionid = currentQuestionid+5 WHERE userId ='1'

我想在一次数据库命中中实现这两个查询,以提高性能。

有没有办法把两者结合起来。我正在使用 WAMP,代码是用 php 脚本编写的。

感谢任何帮助。

我想我找到了答案。

为了将多个查询组合在一起,我们可以使用 mysqli_multi_query() 函数。它适用于 MySQL 服务器版本 4.1.3 及更高版本。它以多个查询作为输入参数并在 one db hit.

中执行它们

例如:

//sql queries should be separated by semi-colons

$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";

// Execute multi query
if (mysqli_multi_query($con,$sql))
{
  do
    {
    // Store first result set
    if ($result=mysqli_store_result($con)) {
      // Fetch row one and one
      while ($row=mysqli_fetch_row($result))
        {
        printf("%s\n",$row[0]);
        }

      // Free result set
      mysqli_free_result($result);
      }
    }
  while (mysqli_next_result($con));
}

来源:http://www.w3schools.com/php/func_mysqli_multi_query.asp