繁体   English   中英

结合两个不同的选择和更新语句

[英]Combine two different select and update statements

我有一个选择查询和一个更新查询,我想将两者结合起来。

选择查询类似于:

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 ,这个查询试图获取从所有问题描述myTablequestionId介于currentQuestionidcurrentQuestionid+4currentQuestionid是在特定于用户的列userTable )。 稍后,我将在前端使用此结果。

现在,我想将currentQuesionid更新为currentQuestionid+5 可以使用以下方法实现:

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

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

有什么办法可以将两者结合起来。 我正在使用WAMP,代码是用php脚本编写的。

任何帮助表示赞赏。

我想我找到了答案。

为了将多个查询组合在一起,我们可以使用mysqli_multi_query()函数。 它适用于MySQL服务器版本4.1.3和更高版本。 它以多个查询作为输入参数,并在一个数据库命中中执行它们。

例如:

 //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

暂无
暂无

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

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